]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codelab.c
Cosmetic changes
[cc65] / src / cc65 / codelab.c
index bc52e5878688cf68cc811ff7a31712f58da8b301..89b45b0e6d4d9a0fb45699aad9460ea584fa11db 100644 (file)
@@ -1,6 +1,6 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                 label.c                                  */
+/*                                codelab.c                                 */
 /*                                                                           */
 /*                          Code label structure                            */
 /*                                                                           */
 
 
 /* common */
+#include "check.h"
 #include "xmalloc.h"
 
 /* cc65 */
-#include "label.h"
+#include "codeent.h"
+#include "codelab.h"
 
 
 
@@ -82,22 +84,46 @@ void FreeCodeLabel (CodeLabel* L)
 
 
 
-unsigned RemoveLabelRef (CodeLabel* L, const struct CodeEntry* E)
-/* Remove a reference to this label, return the number of remaining references */
+void AddLabelRef (CodeLabel* L, struct CodeEntry* E)
+/* Let the CodeEntry E reference the label L */
 {
-    /* Delete the item */
-    CollDeleteItem (&L->JumpFrom, E);
+    /* The insn at E jumps to this label */
+    E->JumpTo = L;
 
-    /* Return the number of remaining references */
-    return CollCount (&L->JumpFrom);
+    /* Remember that in the label */
+    CollAppend (&L->JumpFrom, E);
 }
 
 
 
-void OutputCodeLabel (FILE* F, const CodeLabel* L)
+void MoveLabelRefs (CodeLabel* OldLabel, CodeLabel* NewLabel)
+/* Move all references to OldLabel to point to NewLabel. OldLabel will have no
+ * more references on return.
+ */
+{
+    /* Walk through all instructions referencing the old label */
+    unsigned Count = CollCount (&OldLabel->JumpFrom);
+    while (Count--) {
+
+       /* 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 (const CodeLabel* L, FILE* F)
 /* Output the code label to a file */
 {
-    fprintf (F, "%s:\n", L->Name);
+    fprintf (F, "%s:", L->Name);
 }