]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeseg.c
cleanup
[cc65] / src / cc65 / codeseg.c
index 264bee2f7ca1c2aace6c437fdfeae07e8727c414..ba89ccddad4742ba07d0c63b81ea2f2c3c532e84 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 2001      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 /* common */
 #include "chartype.h"
 #include "check.h"
+#include "global.h"
 #include "hashstr.h"
 #include "strutil.h"
 #include "xmalloc.h"
 #include "xsprintf.h"
 
 /* cc65 */
+#include "asmlabel.h"
 #include "codeent.h"
 #include "codeinfo.h"
+#include "datatype.h"
 #include "error.h"
+#include "symentry.h"
 #include "codeseg.h"
 
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                            Helper functions                              */
 /*****************************************************************************/
 
 
 
-/* Pointer to current code segment */
-CodeSeg* CS = 0;
+static void MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
+/* Move the labels of the code entry E to the label pool of the code segment */
+{
+    unsigned LabelCount = GetCodeLabelCount (E);
+    while (LabelCount--) {
+       CodeLabel* L = GetCodeLabel (E, LabelCount);
+       L->Owner = 0;
+       CollAppend (&S->Labels, L);
+    }
+    CollDeleteAll (&E->Labels);
+}
 
 
 
-/*****************************************************************************/
-/*                   Functions for parsing instructions                     */
-/*****************************************************************************/
+static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
+/* Find the label with the given name. Return the label or NULL if not found */
+{
+    /* Get the first hash chain entry */
+    CodeLabel* L = S->LabelHash[Hash];
+
+    /* Search the list */
+    while (L) {
+       if (strcmp (Name, L->Name) == 0) {
+           /* Found */
+           break;
+       }
+       L = L->Next;
+    }
+    return L;
+}
 
 
 
@@ -85,6 +111,38 @@ static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
 
 
 
+static void RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
+/* Remove the given code label from the hash list */
+{
+    /* Get the first entry in the hash chain */
+    CodeLabel* List = S->LabelHash[L->Hash];
+    CHECK (List != 0);
+
+    /* First, remove the label from the hash chain */
+    if (List == L) {
+               /* First entry in hash chain */
+               S->LabelHash[L->Hash] = L->Next;
+    } else {
+               /* Must search through the chain */
+               while (List->Next != L) {
+                   /* If we've reached the end of the chain, something is *really* wrong */
+                   CHECK (List->Next != 0);
+                   /* Next entry */
+                   List = List->Next;
+               }
+               /* The next entry is the one, we have been searching for */
+               List->Next = L->Next;
+    }
+}
+
+
+
+/*****************************************************************************/
+/*                   Functions for parsing instructions                     */
+/*****************************************************************************/
+
+
+
 static const char* SkipSpace (const char* S)
 /* Skip white space and return an updated pointer */
 {
@@ -97,7 +155,7 @@ static const char* SkipSpace (const char* S)
 
 
 static const char* ReadToken (const char* L, const char* Term,
-                             char* Buf, unsigned BufSize)
+                             char* Buf, unsigned BufSize)
 /* Read the next token into Buf, return the updated line pointer. The
  * token is terminated by one of the characters given in term.
  */
@@ -126,7 +184,7 @@ static const char* ReadToken (const char* L, const char* Term,
 
 
 
-static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
+static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
 /* Parse an instruction nnd generate a code entry from it. If the line contains
  * errors, output an error message and return NULL.
  * For simplicity, we don't accept the broad range of input a "real" assembler
@@ -139,7 +197,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
     am_t               AM = 0;         /* Initialize to keep gcc silent */
     char               Arg[64];
     char               Reg;
-    CodeEntry*         E;
+    CodeEntry*         E;
     CodeLabel*         Label;
 
     /* Mnemonic */
@@ -238,8 +296,15 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
            /* Absolute, maybe indexed */
            L = ReadToken (L, ",", Arg, sizeof (Arg));
            if (*L == '\0') {
-               /* Assume absolute */
-               AM = AM_ABS;
+               /* Absolute, zeropage or branch */
+               if ((OPC->Info & OF_BRA) != 0) {
+                   /* Branch */
+                   AM = AM_BRA;
+               } else if (IsZPName (Arg)) {
+                   AM = AM_ZP;
+               } else {
+                   AM = AM_ABS;
+               }
            } else if (*L == ',') {
                /* Indexed */
                L = SkipSpace (L+1);
@@ -250,7 +315,11 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
                    Reg = toupper (*L);
                    L = SkipSpace (L+1);
                    if (Reg == 'X') {
-                       AM = AM_ABSX;
+                       if (IsZPName (Arg)) {
+                           AM = AM_ZPX;
+                       } else {
+                           AM = AM_ABSX;
+                       }
                    } else if (Reg == 'Y') {
                        AM = AM_ABSY;
                    } else {
@@ -271,18 +340,10 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
      * if it does not exist. Ignore anything but local labels here.
      */
     Label = 0;
-    if ((OPC->Info & CI_MASK_BRA) == CI_BRA && Arg[0] == 'L') {
-
-       unsigned Hash;
-
-       /* Addressing mode must be alsobute or something is really wrong */
-       CHECK (AM == AM_ABS);
-
-       /* Addressing mode is a branch/jump */
-       AM = AM_BRA;
+    if (AM == AM_BRA && Arg[0] == 'L') {
 
        /* Generate the hash over the label, then search for the label */
-       Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
+       unsigned Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
        Label = FindCodeLabel (S, Arg, Hash);
 
        /* If we don't have the label, it's a forward ref - create it */
@@ -295,7 +356,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
     /* We do now have the addressing mode in AM. Allocate a new CodeEntry
      * structure and initialize it.
      */
-    E = NewCodeEntry (OPC, AM, Arg, Label);
+    E = NewCodeEntry (OPC->OPC, AM, Arg, Label, LI);
 
     /* Return the new code entry */
     return E;
@@ -304,12 +365,12 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
 
-CodeSeg* NewCodeSeg (const char* SegName, const char* FuncName)
+CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
 /* Create a new code segment, initialize and return it */
 {
     unsigned I;
@@ -318,57 +379,30 @@ CodeSeg* NewCodeSeg (const char* SegName, const char* FuncName)
     CodeSeg* S = xmalloc (sizeof (CodeSeg));
 
     /* Initialize the fields */
-    S->Next     = 0;
     S->SegName  = xstrdup (SegName);
-    S->FuncName        = xstrdup (FuncName);
+    S->Func    = Func;
     InitCollection (&S->Entries);
     InitCollection (&S->Labels);
     for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
        S->LabelHash[I] = 0;
     }
 
-    /* Return the new struct */
-    return S;
-}
-
-
-
-void FreeCodeSeg (CodeSeg* S)
-/* Free a code segment including all code entries */
-{
-    FAIL ("Not implemented");
-}
-
-
-
-void PushCodeSeg (CodeSeg* S)
-/* Push the given code segment onto the stack */
-{
-    S->Next = CS;
-    CS     = S;
-}
-
-
-
-CodeSeg* PopCodeSeg (void)
-/* Remove the current code segment from the stack and return it */
-{
-    /* Remember the current code segment */
-    CodeSeg* S = CS;
-
-    /* Cannot pop on empty stack */
-    PRECONDITION (S != 0);
-
-    /* Pop */
-    CS = S->Next;
+    /* If we have a function given, get the return type of the function.
+     * Assume ANY return type besides void will use the A and X registers.
+     */
+    if (S->Func && !IsTypeVoid (GetFuncReturn (Func->Type))) {
+       S->ExitRegs = REG_AX;
+    } else {
+       S->ExitRegs = REG_NONE;
+    }
 
-    /* Return the popped code segment */
+    /* Return the new struct */
     return S;
 }
 
 
 
-void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
+void AddCodeEntry (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
 /* Add a line to the given code segment */
 {
     const char* L;
@@ -376,11 +410,8 @@ void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
     char       Token[64];
 
     /* Format the line */
-    va_list ap;
     char Buf [256];
-    va_start (ap, Format);
     xvsprintf (Buf, sizeof (Buf), Format, ap);
-    va_end (ap);
 
     /* Skip whitespace */
     L = SkipSpace (Buf);
@@ -395,7 +426,7 @@ void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
 
        case ';':
            /* Comment or hint, ignore it for now */
-           break;
+           break;
 
        case '.':
            /* Control instruction */
@@ -404,7 +435,7 @@ void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
            break;
 
        default:
-           E = ParseInsn (S, L);
+           E = ParseInsn (S, LI, L);
            break;
     }
 
@@ -415,14 +446,12 @@ void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
        unsigned I;
        unsigned LabelCount = CollCount (&S->Labels);
        for (I = 0; I < LabelCount; ++I) {
+
            /* Get the label */
            CodeLabel* L = CollAt (&S->Labels, I);
-           /* Mark it as defined */
-           L->Flags |= LF_DEF;
-           /* Move it to the code entry */
-           CollAppend (&E->Labels, L);
-           /* Tell the label about it's owner */
-           L->Owner = E;
+
+           /* Attach it to the entry */
+           AttachCodeLabel (E, L);
        }
 
        /* Delete the transfered labels */
@@ -436,34 +465,60 @@ void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
 
 
 
-void DelCodeSegLine (CodeSeg* S, unsigned Index)
-/* Delete an entry from the code segment. This includes deleting any associated
+void InsertCodeEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
+/* Insert the code entry at the index given. Following code entries will be
+ * moved to slots with higher indices.
+ */
+{
+    /* Insert the entry into the collection */
+    CollInsert (&S->Entries, E, Index);
+}
+
+
+
+void DelCodeEntry (CodeSeg* S, unsigned Index)
+/* Delete an entry from the code segment. This includes moving any associated
  * labels, removing references to labels and even removing the referenced labels
  * if the reference count drops to zero.
  */
 {
     /* Get the code entry for the given index */
-    CodeEntry* E = CollAt (&S->Entries, Index);
+    CodeEntry* E = GetCodeEntry (S, Index);
+
+    /* If the entry has a labels, we have to move this label to the next insn.
+     * If there is no next insn, move the label into the code segement label
+     * pool. The operation is further complicated by the fact that the next
+     * insn may already have a label. In that case change all reference to
+     * this label and delete the label instead of moving it.
+     */
+    unsigned Count = GetCodeLabelCount (E);
+    if (Count > 0) {
+
+       /* The instruction has labels attached. Check if there is a next
+        * instruction.
+        */
+       if (Index == GetCodeEntryCount (S)-1) {
+
+           /* No next instruction, move to the codeseg label pool */
+           MoveLabelsToPool (S, E);
+
+       } else {
+
+           /* There is a next insn, get it */
+           CodeEntry* N = GetCodeEntry (S, Index+1);
+
+           /* Move labels to the next entry */
+           MoveCodeLabels (S, E, N);
 
-    /* Remove any labels associated with this entry */
-    unsigned Count;
-    while ((Count = CollCount (&E->Labels)) > 0) {
-       DelCodeLabel (S, CollAt (&E->Labels, Count-1));
+       }
     }
 
     /* If this insn references a label, remove the reference. And, if the
      * the reference count for this label drops to zero, remove this label.
      */
     if (E->JumpTo) {
-
-       /* Remove the reference */
-               if (RemoveLabelRef (E->JumpTo, E) == 0) {
-           /* No references remaining, remove the label */
-           DelCodeLabel (S, E->JumpTo);
-       }
-
-       /* Reset the label pointer to avoid problems later */
-       E->JumpTo = 0;
+               /* Remove the reference */
+               RemoveCodeLabelRef (S, E);
     }
 
     /* Delete the pointer to the insn */
@@ -475,6 +530,90 @@ void DelCodeSegLine (CodeSeg* S, unsigned Index)
 
 
 
+void DelCodeEntries (CodeSeg* S, unsigned Start, unsigned Count)
+/* Delete a range of code entries. This includes removing references to labels,
+ * labels attached to the entries and so on.
+ */
+{
+    /* Start deleting the entries from the rear, because this involves less
+     * memory moving.
+     */
+    while (Count--) {
+       DelCodeEntry (S, Start + Count);
+    }
+}
+
+
+
+void MoveCodeEntry (CodeSeg* S, unsigned OldPos, unsigned NewPos)
+/* Move an entry from one position to another. OldPos is the current position
+ * of the entry, NewPos is the new position of the entry.
+ */
+{
+    /* Get the code entry and remove it from the collection */
+    CodeEntry* E = GetCodeEntry (S, OldPos);
+    CollDelete (&S->Entries, OldPos);
+
+    /* Correct NewPos if needed */
+    if (NewPos >= OldPos) {
+       /* Position has changed with removal */
+       --NewPos;
+    }
+
+    /* Now insert it at the new position */
+    CollInsert (&S->Entries, E, NewPos);
+}
+
+
+
+struct CodeEntry* GetNextCodeEntry (CodeSeg* S, unsigned Index)
+/* Get the code entry following the one with the index Index. If there is no
+ * following code entry, return NULL.
+ */
+{
+    if (Index >= CollCount (&S->Entries)-1) {
+       /* This is the last entry */
+       return 0;
+    } else {
+       /* Code entries left */
+               return CollAtUnchecked (&S->Entries, Index+1);
+    }
+}
+
+
+
+int GetCodeEntries (CodeSeg* S, struct CodeEntry** List,
+                           unsigned Start, unsigned Count)
+/* Get Count code entries into List starting at index start. Return true if
+ * we got the lines, return false if not enough lines were available.
+ */
+{
+    /* Check if enough entries are available */
+    if (Start + Count > CollCount (&S->Entries)) {
+       return 0;
+    }
+
+    /* Copy the entries */
+    while (Count--) {
+       *List++ = CollAtUnchecked (&S->Entries, Start++);
+    }
+
+    /* We have the entries */
+    return 1;
+}
+
+
+
+unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
+/* Return the index of a code entry */
+{
+    int Index = CollIndex (&S->Entries, E);
+    CHECK (Index >= 0);
+    return Index;
+}
+
+
+
 void AddCodeLabel (CodeSeg* S, const char* Name)
 /* Add a code label for the next instruction to follow */
 {
@@ -493,35 +632,58 @@ void AddCodeLabel (CodeSeg* S, const char* Name)
        L = NewCodeSegLabel (S, Name, Hash);
     }
 
+    /* Safety. This call is quite costly, but safety is better */
+    if (CollIndex (&S->Labels, L) >= 0) {
+       Internal ("AddCodeLabel: Label `%s' already defined", Name);
+    }
+
     /* We do now have a valid label. Remember it for later */
     CollAppend (&S->Labels, L);
 }
 
 
 
+CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
+/* If the code entry E does already have a label, return it. Otherwise
+ * create a new label, attach it to E and return it.
+ */
+{
+    CodeLabel* L;
+
+    if (CodeEntryHasLabel (E)) {
+
+       /* Get the label from this entry */
+       L = GetCodeLabel (E, 0);
+
+    } else {
+
+       /* Get a new name */
+       const char* Name = LocalLabelName (GetLocalLabel ());
+
+       /* Generate the hash over the name */
+       unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
+
+       /* Create a new label */
+       L = NewCodeSegLabel (S, Name, Hash);
+
+       /* Attach this label to the code entry */
+       AttachCodeLabel (E, L);
+
+    }
+
+    /* Return the label */
+    return L;
+}
+
+
+
 void DelCodeLabel (CodeSeg* S, CodeLabel* L)
 /* Remove references from this label and delete it. */
 {
     unsigned Count, I;
 
-    /* Get the first entry in the hash chain */
-    CodeLabel* List = S->LabelHash[L->Hash];
-
     /* First, remove the label from the hash chain */
-    if (List == L) {
-               /* First entry in hash chain */
-               S->LabelHash[L->Hash] = L->Next;
-    } else {
-               /* Must search through the chain */
-               while (List->Next != L) {
-                   /* If we've reached the end of the chain, something is *really* wrong */
-                   CHECK (List->Next != 0);
-                   /* Next entry */
-                   List = List->Next;
-               }
-               /* The next entry is the one, we have been searching for */
-               List->Next = L->Next;
-    }
+    RemoveLabelFromHash (S, L);
 
     /* Remove references from insns jumping to this label */
     Count = CollCount (&L->JumpFrom);
@@ -533,8 +695,14 @@ void DelCodeLabel (CodeSeg* S, CodeLabel* L)
     }
     CollDeleteAll (&L->JumpFrom);
 
-    /* Remove the reference to the owning instruction */
-    CollDeleteItem (&L->Owner->Labels, L);
+    /* Remove the reference to the owning instruction if it has one. The
+     * function may be called for a label without an owner when deleting
+     * unfinished parts of the code. This is unfortunate since it allows
+     * errors to slip through.
+     */
+    if (L->Owner) {
+       CollDeleteItem (&L->Owner->Labels, L);
+    }
 
     /* All references removed, delete the label itself */
     FreeCodeLabel (L);
@@ -542,191 +710,275 @@ void DelCodeLabel (CodeSeg* S, CodeLabel* L)
 
 
 
-void AddCodeSegHint (CodeSeg* S, unsigned Hint)
-/* Add a hint for the preceeding instruction */
+void MergeCodeLabels (CodeSeg* S)
+/* Merge code labels. That means: For each instruction, remove all labels but
+ * one and adjust references accordingly.
+ */
 {
-    CodeEntry* E;
+    unsigned I;
 
-    /* Get the number of entries in this segment */
-    unsigned EntryCount = CollCount (&S->Entries);
+    /* Walk over all code entries */
+    unsigned EntryCount = GetCodeEntryCount (S);
+    for (I = 0; I < EntryCount; ++I) {
 
-    /* Must have at least one entry */
-    CHECK (EntryCount > 0);
+               CodeLabel* RefLab;
+       unsigned   J;
 
-    /* Get the last entry */
-    E = CollAt (&S->Entries, EntryCount-1);
+       /* Get a pointer to the next entry */
+       CodeEntry* E = GetCodeEntry (S, I);
 
-    /* Add the hint */
-    E->Hints |= Hint;
-}
+       /* If this entry has zero labels, continue with the next one */
+       unsigned LabelCount = GetCodeLabelCount (E);
+       if (LabelCount == 0) {
+           continue;
+       }
 
+       /* We have at least one label. Use the first one as reference label. */
+       RefLab = GetCodeLabel (E, 0);
 
+       /* Walk through the remaining labels and change references to these
+        * labels to a reference to the one and only label. Delete the labels
+        * that are no longer used. To increase performance, walk backwards
+        * through the list.
+        */
+       for (J = LabelCount-1; J >= 1; --J) {
 
-void DelCodeSegAfter (CodeSeg* S, unsigned Last)
-/* Delete all entries including the given one */
-{
-    /* Get the number of entries in this segment */
-    unsigned Count = CollCount (&S->Entries);
+           /* Get the next label */
+           CodeLabel* L = GetCodeLabel (E, J);
 
-    /* Remove all entries after the given one */
-    while (Last < Count) {
+           /* Move all references from this label to the reference label */
+           MoveLabelRefs (L, RefLab);
 
-       /* Get the next entry */
-       CodeEntry* E = CollAt (&S->Entries, Count-1);
-
-       /* We have to transfer all labels to the code segment label pool */
-       unsigned LabelCount = CollCount (&E->Labels);
-       while (LabelCount--) {
-           CodeLabel* L = CollAt (&E->Labels, LabelCount);
-           L->Flags &= ~LF_DEF;
-           CollAppend (&S->Labels, L);
-       }
-       CollDeleteAll (&E->Labels);
+                   /* Remove the label completely. */
+                   DelCodeLabel (S, L);
+       }
 
-       /* Remove the code entry */
-       FreeCodeEntry (CollAt (&S->Entries, Count-1));
-       CollDelete (&S->Entries, Count-1);
-       --Count;
+       /* The reference label is the only remaining label. Check if there
+        * are any references to this label, and delete it if this is not
+        * the case.
+        */
+               if (CollCount (&RefLab->JumpFrom) == 0) {
+           /* Delete the label */
+                   DelCodeLabel (S, RefLab);
+       }
     }
 }
 
 
 
-void OutputCodeSeg (FILE* F, const CodeSeg* S)
-/* Output the code segment data to a file */
+void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
+/* Move all labels from Old to New. The routine will move the labels itself
+ * if New does not have any labels, and move references if there is at least
+ * a label for new. If references are moved, the old label is deleted
+ * afterwards.
+ */
 {
-    unsigned I;
+    /* Get the number of labels to move */
+    unsigned OldLabelCount = GetCodeLabelCount (Old);
 
-    /* Get the number of entries in this segment */
-    unsigned Count = CollCount (&S->Entries);
-
-    fprintf (F, "; Labels: ");
-    for (I = 0; I < CS_LABEL_HASH_SIZE; ++I) {
-       const CodeLabel* L = S->LabelHash[I];
-       while (L) {
-           fprintf (F, "%s ", L->Name);
-           L = L->Next;
-       }
-    }
-    fprintf (F, "\n");
+    /* Does the new entry have itself a label? */
+    if (CodeEntryHasLabel (New)) {
 
-    /* Output the segment directive */
-    fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
+       /* The new entry does already have a label - move references */
+       CodeLabel* NewLabel = GetCodeLabel (New, 0);
+       while (OldLabelCount--) {
 
-    /* If this is a segment for a function, enter a function */
-    if (S->FuncName[0] != '\0') {
-       fprintf (F, ".proc\t_%s\n\n", S->FuncName);
-    }
+           /* Get the next label */
+           CodeLabel* OldLabel = GetCodeLabel (Old, OldLabelCount);
 
-    /* Output all entries */
-    for (I = 0; I < Count; ++I) {
-       OutputCodeEntry (F, CollConstAt (&S->Entries, I));
-    }
+           /* Move references */
+           MoveLabelRefs (OldLabel, NewLabel);
+
+           /* Delete the label */
+           DelCodeLabel (S, OldLabel);
+
+       }
+
+    } else {
+
+       /* The new entry does not have a label, just move them */
+       while (OldLabelCount--) {
+
+           /* Move the label to the new entry */
+           MoveCodeLabel (GetCodeLabel (Old, OldLabelCount), New);
+
+       }
 
-    /* If this is a segment for a function, leave the function */
-    if (S->FuncName[0] != '\0') {
-       fprintf (F, "\n.endproc\n\n");
     }
 }
 
 
 
-CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
-/* Find the label with the given name. Return the label or NULL if not found */
+void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
+/* Remove the reference between E and the label it jumps to. The reference
+ * will be removed on both sides and E->JumpTo will be 0 after that. If
+ * the reference was the only one for the label, the label will get
+ * deleted.
+ */
 {
-    /* Get the first hash chain entry */
-    CodeLabel* L = S->LabelHash[Hash];
+    /* Get a pointer to the label and make sure it exists */
+    CodeLabel* L = E->JumpTo;
+    CHECK (L != 0);
 
-    /* Search the list */
-    while (L) {
-       if (strcmp (Name, L->Name) == 0) {
-           /* Found */
-           break;
-       }
-       L = L->Next;
+    /* Delete the entry from the label */
+    CollDeleteItem (&L->JumpFrom, E);
+
+    /* The entry jumps no longer to L */
+    E->JumpTo = 0;
+
+    /* If there are no more references, delete the label */
+    if (CollCount (&L->JumpFrom) == 0) {
+               DelCodeLabel (S, L);
     }
-    return L;
 }
 
 
 
-void MergeCodeLabels (CodeSeg* S)
-/* Merge code labels. That means: For each instruction, remove all labels but
- * one and adjust the code entries accordingly.
+void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
+/* Change the reference of E to L instead of the current one. If this
+ * was the only reference to the old label, the old label will get
+ * deleted.
  */
 {
-    unsigned I;
+    /* Get the old label */
+    CodeLabel* OldLabel = E->JumpTo;
 
-    /* Walk over all code entries */
-    unsigned EntryCount = CollCount (&S->Entries);
-    for (I = 0; I < EntryCount; ++I) {
+    /* Be sure that code entry references a label */
+    PRECONDITION (OldLabel != 0);
 
-               CodeLabel* RefLab;
-       unsigned   J;
+    /* Remove the reference to our label */
+    RemoveCodeLabelRef (S, E);
 
-       /* Get a pointer to the next entry */
-       CodeEntry* E = CollAt (&S->Entries, I);
+    /* Use the new label */
+    AddLabelRef (L, E);
+}
 
-       /* If this entry has zero labels, continue with the next one */
-       unsigned LabelCount = CollCount (&E->Labels);
-       if (LabelCount == 0) {
-           continue;
-       }
 
-       /* We have at least one label. Use the first one as reference label. */
-       RefLab = CollAt (&E->Labels, 0);
 
-       /* Walk through the remaining labels and change references to these
-        * labels to a reference to the one and only label. Delete the labels
-        * that are no longer used. To increase performance, walk backwards
-        * through the list.
-        */
-       for (J = LabelCount-1; J >= 1; --J) {
+void DelCodeSegAfter (CodeSeg* S, unsigned Last)
+/* Delete all entries including the given one */
+{
+    /* Get the number of entries in this segment */
+    unsigned Count = GetCodeEntryCount (S);
 
-           unsigned K;
+    /* First pass: Delete all references to labels. If the reference count
+     * for a label drops to zero, delete it.
+     */
+    unsigned C = Count;
+    while (Last < C--) {
 
-           /* Get the next label */
-           CodeLabel* L = CollAt (&E->Labels, J);
+       /* Get the next entry */
+       CodeEntry* E = GetCodeEntry (S, C);
+
+       /* Check if this entry has a label reference */
+       if (E->JumpTo) {
+           /* If the label is a label in the label pool and this is the last
+            * reference to the label, remove the label from the pool.
+            */
+           CodeLabel* L = E->JumpTo;
+           int Index = CollIndex (&S->Labels, L);
+           if (Index >= 0 && CollCount (&L->JumpFrom) == 1) {
+               /* Delete it from the pool */
+               CollDelete (&S->Labels, Index);
+           }
 
-           /* Walk through all instructions referencing this label */
-           unsigned RefCount = CollCount (&L->JumpFrom);
-           for (K = 0; K < RefCount; ++K) {
+           /* Remove the reference to the label */
+           RemoveCodeLabelRef (S, E);
+       }
 
-               /* Get the next instruction that references this label */
-               CodeEntry* E = CollAt (&L->JumpFrom, K);
+    }
 
-               /* Change the reference */
-               CHECK (E->JumpTo == L);
-               AddLabelRef (RefLab, E);
+    /* Second pass: Delete the instructions. If a label attached to an
+     * instruction still has references, it must be references from outside
+     * the deleted area. Don't delete the label in this case, just make it
+     * ownerless and move it to the label pool.
+     */
+    C = Count;
+    while (Last < C--) {
 
-           }
+       /* Get the next entry */
+       CodeEntry* E = GetCodeEntry (S, C);
 
-           /* There are no more instructions jumping to this label now */
-           CollDeleteAll (&L->JumpFrom);
+       /* Check if this entry has a label attached */
+       if (CodeEntryHasLabel (E)) {
+           /* Move the labels to the pool and clear the owner pointer */
+           MoveLabelsToPool (S, E);
+       }
 
-                   /* Remove the label completely. */
-                   DelCodeLabel (S, L);
-       }
+       /* Delete the pointer to the entry */
+       CollDelete (&S->Entries, C);
 
-       /* The reference label is the only remaining label. Check if there
-        * are any references to this label, and delete it if this is not
-        * the case.
-        */
-               if (CollCount (&RefLab->JumpFrom) == 0) {
-           /* Delete the label */
-                   DelCodeLabel (S, RefLab);
-       }
+       /* Delete the entry itself */
+       FreeCodeEntry (E);
     }
 }
 
 
 
-unsigned GetCodeSegEntries (const CodeSeg* S)
-/* Return the number of entries for the given code segment */
+void OutputCodeSeg (const CodeSeg* S, FILE* F)
+/* Output the code segment data to a file */
 {
-    return CollCount (&S->Entries);
+    unsigned I;
+    const LineInfo* LI;
+
+    /* Get the number of entries in this segment */
+    unsigned Count = GetCodeEntryCount (S);
+
+    /* If the code segment is empty, bail out here */
+    if (Count == 0) {
+       return;
+    }
+
+    /* Output the segment directive */
+    fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
+
+    /* If this is a segment for a function, enter a function */
+    if (S->Func) {
+       fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
+    }
+
+    /* Output all entries, prepended by the line information if it has changed */
+    LI = 0;
+    for (I = 0; I < Count; ++I) {
+       /* Get the next entry */
+       const CodeEntry* E = CollConstAt (&S->Entries, I);
+       /* Check if the line info has changed. If so, output the source line
+        * if the option is enabled and output debug line info if the debug
+        * option is enabled.
+        */
+       if (E->LI != LI) {
+           /* Line info has changed, remember the new line info */
+           LI = E->LI;
+
+           /* Add the source line as a comment */
+           if (AddSource) {
+               fprintf (F, ";\n; %s\n;\n", LI->Line);
+           }
+
+           /* Add line debug info */
+           if (DebugInfo) {
+               fprintf (F, "\t.dbg\tline, \"%s\", %u\n",
+                        GetInputName (LI), GetInputLine (LI));
+           }
+       }
+       /* Output the code */
+       OutputCodeEntry (E, F);
+    }
+
+    /* If debug info is enabled, terminate the last line number information */
+    if (DebugInfo) {
+       fprintf (F, "\t.dbg\tline\n");
+    }
+
+    /* If this is a segment for a function, leave the function */
+    if (S->Func) {
+       fprintf (F, "\n.endproc\n\n");
+    }
 }
 
 
 
 
+
+
+