]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeseg.c
Collect more info on zp registers
[cc65] / src / cc65 / codeseg.c
index ae4e0d2c8d46a209b8f57b5a03c20a864f1b1ea2..cc4ee7364045097cf91e7b2a67b2d0f49db22fd3 100644 (file)
 
 
 
-static void MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
+static void CS_MoveLabelsToEntry (CodeSeg* S, CodeEntry* E)
+/* Move all labels from the label pool to the given entry and remove them
+ * from the pool.
+ */
+{
+    /* Transfer the labels if we have any */
+    unsigned I;
+    unsigned LabelCount = CollCount (&S->Labels);
+    for (I = 0; I < LabelCount; ++I) {
+
+       /* Get the label */
+       CodeLabel* L = CollAt (&S->Labels, I);
+
+       /* Attach it to the entry */
+       CE_AttachLabel (E, L);
+    }
+
+    /* Delete the transfered labels */
+    CollDeleteAll (&S->Labels);
+}
+
+
+
+static void CS_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);
+    unsigned LabelCount = CE_GetLabelCount (E);
     while (LabelCount--) {
-       CodeLabel* L = GetCodeLabel (E, LabelCount);
+       CodeLabel* L = CE_GetLabel (E, LabelCount);
        L->Owner = 0;
        CollAppend (&S->Labels, L);
     }
@@ -76,7 +99,7 @@ static void MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
 
 
 
-static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
+static CodeLabel* CS_FindLabel (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 */
@@ -95,10 +118,10 @@ static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
 
 
 
-static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
+static CodeLabel* CS_NewCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
 /* Create a new label and insert it into the label hash table */
 {
-    /* Not found - create a new one */
+    /* Create a new label */
     CodeLabel* L = NewCodeLabel (Name, Hash);
 
     /* Enter the label into the hash table */
@@ -111,7 +134,7 @@ static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
 
 
 
-static void RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
+static void CS_RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
 /* Remove the given code label from the hash list */
 {
     /* Get the first entry in the hash chain */
@@ -194,9 +217,9 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
 {
     char                       Mnemo[16];
     const OPCDesc*     OPC;
-    am_t               AM = 0;         /* Initialize to keep gcc silent */
-    char               Arg[64];
-    char               Reg;
+    am_t               AM = 0;         /* Initialize to keep gcc silent */
+    char               Arg[64];
+    char               Reg;
     CodeEntry*         E;
     CodeLabel*         Label;
 
@@ -235,7 +258,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
            L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
 
            /* Check for errors */
-           if (*L == '\0') {
+           if (*L == '\0') {
                Error ("ASM code error: syntax error");
                return 0;
            }
@@ -254,7 +277,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
                    return 0;
                }
                L = SkipSpace (L+1);
-               if (*L != '\0') {
+               if (*L != '\0') {
                    Error ("ASM code error: syntax error");
                    return 0;
                }
@@ -278,7 +301,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
                    AM = AM65_ZP_IND;
                } else {
                    Error ("ASM code error: syntax error");
-                   return 0;
+                   return 0;
                }
            }
            break;
@@ -297,10 +320,10 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
            L = ReadToken (L, ",", Arg, sizeof (Arg));
            if (*L == '\0') {
                /* Absolute, zeropage or branch */
-               if ((OPC->Info & OF_BRA) != 0) {
+               if ((OPC->Info & OF_BRA) != 0) {
                    /* Branch */
                    AM = AM65_BRA;
-               } else if (IsZPName (Arg)) {
+               } else if (IsZPName (Arg, 0)) {
                    AM = AM65_ZP;
                } else {
                    AM = AM65_ABS;
@@ -315,13 +338,13 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
                    Reg = toupper (*L);
                    L = SkipSpace (L+1);
                    if (Reg == 'X') {
-                       if (IsZPName (Arg)) {
+                       if (IsZPName (Arg, 0)) {
                            AM = AM65_ZPX;
                        } else {
                            AM = AM65_ABSX;
                        }
                    } else if (Reg == 'Y') {
-                       AM = AM65_ABSY;
+                       AM = AM65_ABSY;
                    } else {
                        Error ("ASM code error: syntax error");
                        return 0;
@@ -344,12 +367,12 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
 
        /* Generate the hash over the label, then search for the label */
        unsigned Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
-       Label = FindCodeLabel (S, Arg, Hash);
+       Label = CS_FindLabel (S, Arg, Hash);
 
        /* If we don't have the label, it's a forward ref - create it */
        if (Label == 0) {
            /* Generate a new label */
-           Label = NewCodeSegLabel (S, Arg, Hash);
+           Label = CS_NewCodeLabel (S, Arg, Hash);
        }
     }
 
@@ -374,6 +397,7 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
 /* Create a new code segment, initialize and return it */
 {
     unsigned I;
+    const type* RetType;
 
     /* Allocate memory */
     CodeSeg* S = xmalloc (sizeof (CodeSeg));
@@ -390,8 +414,13 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
     /* 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;
+    RetType = GetFuncReturn (Func->Type);
+    if (S->Func && !IsTypeVoid (RetType)) {
+       if (SizeOf (RetType) == SizeOf (type_long)) {
+           S->ExitRegs = REG_EAX;
+       } else {
+           S->ExitRegs = REG_AX;
+       }
     } else {
        S->ExitRegs = REG_NONE;
     }
@@ -402,12 +431,24 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
 
 
 
-void AddCodeEntry (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
+void CS_AddEntry (CodeSeg* S, struct CodeEntry* E)
+/* Add an entry to the given code segment */
+{
+    /* Transfer the labels if we have any */
+    CS_MoveLabelsToEntry (S, E);
+
+    /* Add the entry to the list of code entries in this segment */
+    CollAppend (&S->Entries, E);
+}
+
+
+
+void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
 /* Add a line to the given code segment */
 {
     const char* L;
     CodeEntry*  E;
-    char       Token[64];
+    char       Token[64];
 
     /* Format the line */
     char Buf [256];
@@ -417,7 +458,7 @@ void AddCodeEntry (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
     L = SkipSpace (Buf);
 
     /* Check which type of instruction we have */
-    E = 0;     /* Assume no insn created */
+    E = 0;     /* Assume no insn created */
     switch (*L) {
 
        case '\0':
@@ -441,31 +482,24 @@ void AddCodeEntry (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
 
     /* If we have a code entry, transfer the labels and insert it */
     if (E) {
+       CS_AddEntry (S, E);
+    }
+}
 
-       /* Transfer the labels if we have any */
-       unsigned I;
-       unsigned LabelCount = CollCount (&S->Labels);
-       for (I = 0; I < LabelCount; ++I) {
-
-           /* Get the label */
-           CodeLabel* L = CollAt (&S->Labels, I);
-
-           /* Attach it to the entry */
-           AttachCodeLabel (E, L);
-       }
-
-       /* Delete the transfered labels */
-       CollDeleteAll (&S->Labels);
 
-       /* Add the entry to the list of code entries in this segment */
-       CollAppend (&S->Entries, E);
 
-    }
+void CS_AddLine (CodeSeg* S, LineInfo* LI, const char* Format, ...)
+/* Add a line to the given code segment */
+{
+    va_list ap;
+    va_start (ap, Format);
+    CS_AddVLine (S, LI, Format, ap);
+    va_end (ap);
 }
 
 
 
-void InsertCodeEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
+void CS_InsertEntry (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.
  */
@@ -476,14 +510,14 @@ void InsertCodeEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
 
 
 
-void DelCodeEntry (CodeSeg* S, unsigned Index)
+void CS_DelEntry (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 = GetCodeEntry (S, Index);
+    CodeEntry* E = CS_GetEntry (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
@@ -491,24 +525,24 @@ void DelCodeEntry (CodeSeg* S, unsigned Index)
      * 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);
+    unsigned Count = CE_GetLabelCount (E);
     if (Count > 0) {
 
        /* The instruction has labels attached. Check if there is a next
         * instruction.
         */
-       if (Index == GetCodeEntryCount (S)-1) {
+       if (Index == CS_GetEntryCount (S)-1) {
 
            /* No next instruction, move to the codeseg label pool */
-           MoveLabelsToPool (S, E);
+           CS_MoveLabelsToPool (S, E);
 
        } else {
 
            /* There is a next insn, get it */
-           CodeEntry* N = GetCodeEntry (S, Index+1);
+           CodeEntry* N = CS_GetEntry (S, Index+1);
 
            /* Move labels to the next entry */
-           MoveCodeLabels (S, E, N);
+           CS_MoveLabels (S, E, N);
 
        }
     }
@@ -518,7 +552,7 @@ void DelCodeEntry (CodeSeg* S, unsigned Index)
      */
     if (E->JumpTo) {
                /* Remove the reference */
-               RemoveCodeLabelRef (S, E);
+               CS_RemoveLabelRef (S, E);
     }
 
     /* Delete the pointer to the insn */
@@ -530,7 +564,7 @@ void DelCodeEntry (CodeSeg* S, unsigned Index)
 
 
 
-void DelCodeEntries (CodeSeg* S, unsigned Start, unsigned Count)
+void CS_DelEntries (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.
  */
@@ -539,51 +573,70 @@ void DelCodeEntries (CodeSeg* S, unsigned Start, unsigned Count)
      * memory moving.
      */
     while (Count--) {
-       DelCodeEntry (S, Start + Count);
+       CS_DelEntry (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.
+void CS_MoveEntries (CodeSeg* S, unsigned Start, unsigned Count, unsigned NewPos)
+/* Move a range of entries from one position to another. Start is the index
+ * of the first entry to move, Count is the number of entries and NewPos is
+ * the index of the target entry. The entry with the index Start will later
+ * have the index NewPos. All entries with indices NewPos and above are
+ * moved to higher indices. If the code block is moved to the end of the
+ * current code, and if pending labels exist, these labels will get attached
+ * to the first instruction of the moved block (the first one after the
+ * current code end)
  */
 {
-    /* 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;
+    /* If NewPos is at the end of the code segment, move any labels from the
+     * label pool to the first instruction of the moved range.
+     */
+    if (NewPos == CS_GetEntryCount (S)) {
+       CS_MoveLabelsToEntry (S, CS_GetEntry (S, Start));
     }
 
-    /* Now insert it at the new position */
-    CollInsert (&S->Entries, E, NewPos);
+    /* Move the code block to the destination */
+    CollMoveMultiple (&S->Entries, Start, Count, NewPos);
+}
+
+
+
+struct CodeEntry* CS_GetPrevEntry (CodeSeg* S, unsigned Index)
+/* Get the code entry preceeding the one with the index Index. If there is no
+ * preceeding code entry, return NULL.
+ */
+{
+    if (Index == 0) {
+       /* This is the first entry */
+       return 0;
+    } else {
+       /* Previous entry available */
+               return CollAtUnchecked (&S->Entries, Index-1);
+    }
 }
 
 
 
-struct CodeEntry* GetNextCodeEntry (CodeSeg* S, unsigned Index)
+struct CodeEntry* CS_GetNextEntry (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;
+       /* This is the last entry */
+       return 0;
     } else {
-       /* Code entries left */
+       /* Code entries left */
                return CollAtUnchecked (&S->Entries, Index+1);
     }
 }
 
 
 
-int GetCodeEntries (CodeSeg* S, struct CodeEntry** List,
-                           unsigned Start, unsigned Count)
+int CS_GetEntries (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.
  */
@@ -604,7 +657,7 @@ int GetCodeEntries (CodeSeg* S, struct CodeEntry** List,
 
 
 
-unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
+unsigned CS_GetEntryIndex (CodeSeg* S, struct CodeEntry* E)
 /* Return the index of a code entry */
 {
     int Index = CollIndex (&S->Entries, E);
@@ -614,14 +667,14 @@ unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
 
 
 
-void AddCodeLabel (CodeSeg* S, const char* Name)
+CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name)
 /* Add a code label for the next instruction to follow */
 {
     /* Calculate the hash from the name */
     unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
 
     /* Try to find the code label if it does already exist */
-    CodeLabel* L = FindCodeLabel (S, Name, Hash);
+    CodeLabel* L = CS_FindLabel (S, Name, Hash);
 
     /* Did we find it? */
     if (L) {
@@ -629,7 +682,7 @@ void AddCodeLabel (CodeSeg* S, const char* Name)
        CHECK (L->Owner == 0);
     } else {
        /* Not found - create a new one */
-       L = NewCodeSegLabel (S, Name, Hash);
+       L = CS_NewCodeLabel (S, Name, Hash);
     }
 
     /* Safety. This call is quite costly, but safety is better */
@@ -639,21 +692,24 @@ void AddCodeLabel (CodeSeg* S, const char* Name)
 
     /* We do now have a valid label. Remember it for later */
     CollAppend (&S->Labels, L);
+
+    /* Return the label */
+    return L;
 }
 
 
 
-CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
+CodeLabel* CS_GenLabel (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)) {
+    if (CE_HasLabel (E)) {
 
        /* Get the label from this entry */
-       L = GetCodeLabel (E, 0);
+       L = CE_GetLabel (E, 0);
 
     } else {
 
@@ -664,10 +720,10 @@ CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
        unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
 
        /* Create a new label */
-       L = NewCodeSegLabel (S, Name, Hash);
+       L = CS_NewCodeLabel (S, Name, Hash);
 
        /* Attach this label to the code entry */
-       AttachCodeLabel (E, L);
+       CE_AttachLabel (E, L);
 
     }
 
@@ -677,13 +733,13 @@ CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
 
 
 
-void DelCodeLabel (CodeSeg* S, CodeLabel* L)
+void CS_DelLabel (CodeSeg* S, CodeLabel* L)
 /* Remove references from this label and delete it. */
 {
     unsigned Count, I;
 
     /* First, remove the label from the hash chain */
-    RemoveLabelFromHash (S, L);
+    CS_RemoveLabelFromHash (S, L);
 
     /* Remove references from insns jumping to this label */
     Count = CollCount (&L->JumpFrom);
@@ -701,7 +757,7 @@ void DelCodeLabel (CodeSeg* S, CodeLabel* L)
      * errors to slip through.
      */
     if (L->Owner) {
-       CollDeleteItem (&L->Owner->Labels, L);
+               CollDeleteItem (&L->Owner->Labels, L);
     }
 
     /* All references removed, delete the label itself */
@@ -710,7 +766,7 @@ void DelCodeLabel (CodeSeg* S, CodeLabel* L)
 
 
 
-void MergeCodeLabels (CodeSeg* S)
+void CS_MergeLabels (CodeSeg* S)
 /* Merge code labels. That means: For each instruction, remove all labels but
  * one and adjust references accordingly.
  */
@@ -718,23 +774,22 @@ void MergeCodeLabels (CodeSeg* S)
     unsigned I;
 
     /* Walk over all code entries */
-    unsigned EntryCount = GetCodeEntryCount (S);
-    for (I = 0; I < EntryCount; ++I) {
+    for (I = 0; I < CS_GetEntryCount (S); ++I) {
 
                CodeLabel* RefLab;
-       unsigned   J;
+               unsigned   J;
 
        /* Get a pointer to the next entry */
-       CodeEntry* E = GetCodeEntry (S, I);
+       CodeEntry* E = CS_GetEntry (S, I);
 
        /* If this entry has zero labels, continue with the next one */
-       unsigned LabelCount = GetCodeLabelCount (E);
+       unsigned LabelCount = CE_GetLabelCount (E);
        if (LabelCount == 0) {
            continue;
        }
 
        /* We have at least one label. Use the first one as reference label. */
-       RefLab = GetCodeLabel (E, 0);
+       RefLab = CE_GetLabel (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
@@ -744,13 +799,13 @@ void MergeCodeLabels (CodeSeg* S)
        for (J = LabelCount-1; J >= 1; --J) {
 
            /* Get the next label */
-           CodeLabel* L = GetCodeLabel (E, J);
+           CodeLabel* L = CE_GetLabel (E, J);
 
            /* Move all references from this label to the reference label */
-           MoveLabelRefs (L, RefLab);
+           CL_MoveRefs (L, RefLab);
 
                    /* Remove the label completely. */
-                   DelCodeLabel (S, L);
+                   CS_DelLabel (S, L);
        }
 
        /* The reference label is the only remaining label. Check if there
@@ -759,14 +814,14 @@ void MergeCodeLabels (CodeSeg* S)
         */
                if (CollCount (&RefLab->JumpFrom) == 0) {
            /* Delete the label */
-                   DelCodeLabel (S, RefLab);
+                   CS_DelLabel (S, RefLab);
        }
     }
 }
 
 
 
-void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
+void CS_MoveLabels (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
@@ -774,23 +829,23 @@ void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
  */
 {
     /* Get the number of labels to move */
-    unsigned OldLabelCount = GetCodeLabelCount (Old);
+    unsigned OldLabelCount = CE_GetLabelCount (Old);
 
     /* Does the new entry have itself a label? */
-    if (CodeEntryHasLabel (New)) {
+    if (CE_HasLabel (New)) {
 
        /* The new entry does already have a label - move references */
-       CodeLabel* NewLabel = GetCodeLabel (New, 0);
+       CodeLabel* NewLabel = CE_GetLabel (New, 0);
        while (OldLabelCount--) {
 
            /* Get the next label */
-           CodeLabel* OldLabel = GetCodeLabel (Old, OldLabelCount);
+           CodeLabel* OldLabel = CE_GetLabel (Old, OldLabelCount);
 
            /* Move references */
-           MoveLabelRefs (OldLabel, NewLabel);
+           CL_MoveRefs (OldLabel, NewLabel);
 
            /* Delete the label */
-           DelCodeLabel (S, OldLabel);
+           CS_DelLabel (S, OldLabel);
 
        }
 
@@ -800,7 +855,7 @@ void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
        while (OldLabelCount--) {
 
            /* Move the label to the new entry */
-           MoveCodeLabel (GetCodeLabel (Old, OldLabelCount), New);
+           CE_MoveLabel (CE_GetLabel (Old, OldLabelCount), New);
 
        }
 
@@ -809,7 +864,7 @@ void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
 
 
 
-void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
+void CS_RemoveLabelRef (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
@@ -828,13 +883,13 @@ void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
 
     /* If there are no more references, delete the label */
     if (CollCount (&L->JumpFrom) == 0) {
-               DelCodeLabel (S, L);
+               CS_DelLabel (S, L);
     }
 }
 
 
 
-void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
+void CS_MoveLabelRef (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.
@@ -847,19 +902,19 @@ void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
     PRECONDITION (OldLabel != 0);
 
     /* Remove the reference to our label */
-    RemoveCodeLabelRef (S, E);
+    CS_RemoveLabelRef (S, E);
 
     /* Use the new label */
-    AddLabelRef (L, E);
+    CL_AddRef (L, E);
 }
 
 
 
-void DelCodeSegAfter (CodeSeg* S, unsigned Last)
+void CS_DelCodeAfter (CodeSeg* S, unsigned Last)
 /* Delete all entries including the given one */
 {
     /* Get the number of entries in this segment */
-    unsigned Count = GetCodeEntryCount (S);
+    unsigned Count = CS_GetEntryCount (S);
 
     /* First pass: Delete all references to labels. If the reference count
      * for a label drops to zero, delete it.
@@ -867,23 +922,23 @@ void DelCodeSegAfter (CodeSeg* S, unsigned Last)
     unsigned C = Count;
     while (Last < C--) {
 
-       /* 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);
-           }
+               /* Get the next entry */
+               CodeEntry* E = CS_GetEntry (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);
+                   }
 
            /* Remove the reference to the label */
-           RemoveCodeLabelRef (S, E);
+           CS_RemoveLabelRef (S, E);
        }
 
     }
@@ -897,12 +952,12 @@ void DelCodeSegAfter (CodeSeg* S, unsigned Last)
     while (Last < C--) {
 
        /* Get the next entry */
-       CodeEntry* E = GetCodeEntry (S, C);
+       CodeEntry* E = CS_GetEntry (S, C);
 
        /* Check if this entry has a label attached */
-       if (CodeEntryHasLabel (E)) {
+       if (CE_HasLabel (E)) {
            /* Move the labels to the pool and clear the owner pointer */
-           MoveLabelsToPool (S, E);
+           CS_MoveLabelsToPool (S, E);
        }
 
        /* Delete the pointer to the entry */
@@ -915,14 +970,14 @@ void DelCodeSegAfter (CodeSeg* S, unsigned Last)
 
 
 
-void OutputCodeSeg (const CodeSeg* S, FILE* F)
+void CS_Output (const CodeSeg* S, FILE* F)
 /* Output the code segment data to a file */
 {
     unsigned I;
     const LineInfo* LI;
 
     /* Get the number of entries in this segment */
-    unsigned Count = GetCodeEntryCount (S);
+    unsigned Count = CS_GetEntryCount (S);
 
     /* If the code segment is empty, bail out here */
     if (Count == 0) {
@@ -952,17 +1007,17 @@ void OutputCodeSeg (const CodeSeg* S, FILE* F)
 
            /* Add the source line as a comment */
            if (AddSource) {
-               fprintf (F, ";\n; %s\n;\n", LI->Line);
+               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));
+               fprintf (F, "\t.dbg\tline, \"%s\", %u\n",
+                        GetInputName (LI), GetInputLine (LI));
            }
        }
        /* Output the code */
-       OutputCodeEntry (E, F);
+       CE_Output (E, F);
     }
 
     /* If debug info is enabled, terminate the last line number information */
@@ -978,7 +1033,230 @@ void OutputCodeSeg (const CodeSeg* S, FILE* F)
 
 
 
+void CS_FreeRegInfo (CodeSeg* S)
+/* Free register infos for all instructions */
+{
+    unsigned I;
+    for (I = 0; I < CS_GetEntryCount (S); ++I) {
+        CE_FreeRegInfo (CS_GetEntry(S, I));
+    }
+}
+
+
+
+void CS_GenRegInfo (CodeSeg* S)
+/* Generate register infos for all instructions */
+{
+    unsigned I;
+    RegContents Regs;
+    RegContents* CurrentRegs;
+    int WasJump;
+
+    /* Be sure to delete all register infos */
+    CS_FreeRegInfo (S);
+
+    /* On entry, the register contents are unknown */
+    RC_Invalidate (&Regs);
+    CurrentRegs = &Regs;
+
+    /* First pass. Walk over all insns an note just the changes from one
+     * insn to the next one.
+     */
+    WasJump = 0;
+    for (I = 0; I < CS_GetEntryCount (S); ++I) {
+
+       CodeEntry* P;
+
+       /* Get the next instruction */
+       CodeEntry* E = CollAtUnchecked (&S->Entries, I);
+
+       /* If the instruction has a label, we need some special handling */
+       unsigned LabelCount = CE_GetLabelCount (E);
+       if (LabelCount > 0) {
+
+           /* Loop over all entry points that jump here. If these entry
+            * points already have register info, check if all values are
+            * known and identical. If all values are identical, and the
+            * preceeding instruction was not an unconditional branch, check
+            * if the register value on exit of the preceeding instruction
+            * is also identical. If all these values are identical, the
+            * value of a register is known, otherwise it is unknown.
+            */
+           CodeLabel* Label = CE_GetLabel (E, 0);
+           unsigned Entry;
+           if (WasJump) {
+               /* Preceeding insn was an unconditional branch */
+               CodeEntry* J = CL_GetRef(Label, 0);
+               if (J->RI) {
+                   Regs = J->RI->Out2;
+               } else {
+                   RC_Invalidate (&Regs);
+               }
+               Entry = 1;
+           } else {
+               Regs = *CurrentRegs;
+               Entry = 0;
+           }
+
+           while (Entry < CL_GetRefCount (Label)) {
+               /* Get this entry */
+               CodeEntry* J = CL_GetRef (Label, Entry);
+               if (J->RI == 0) {
+                   /* No register info for this entry, bail out */
+                   RC_Invalidate (&Regs);
+                   break;
+               }
+               if (J->RI->Out2.RegA != Regs.RegA) {
+                   Regs.RegA = -1;
+               }
+               if (J->RI->Out2.RegX != Regs.RegX) {
+                   Regs.RegX = -1;
+               }
+               if (J->RI->Out2.RegY != Regs.RegY) {
+                   Regs.RegY = -1;
+               }
+               ++Entry;
+           }
+
+           /* Use this register info */
+           CurrentRegs = &Regs;
+
+       }
+
+       /* Generate register info for this instruction */
+        CE_GenRegInfo (E, CurrentRegs);
+
+       /* Remember for the next insn if this insn was an uncondition branch */
+       WasJump = (E->Info & OF_UBRA) != 0;
+
+       /* Output registers for this insn are input for the next */
+       CurrentRegs = &E->RI->Out;
+
+       /* If this insn is a branch on zero flag, we may have more info on
+        * register contents for one of both flow directions, but only if
+        * there is a previous instruction.
+        */
+       if ((E->Info & OF_ZBRA) != 0 && (P = CS_GetPrevEntry (S, I)) != 0) {
+
+           /* Get the branch condition */
+           bc_t BC = GetBranchCond (E->OPC);
+
+           /* Check the previous instruction */
+           switch (P->OPC) {
+
+               case OP65_ADC:
+               case OP65_AND:
+               case OP65_DEA:
+               case OP65_EOR:
+               case OP65_INA:
+               case OP65_LDA:
+               case OP65_ORA:
+               case OP65_PLA:
+               case OP65_SBC:
+                   /* A is zero in one execution flow direction */
+                   if (BC == BC_EQ) {
+                               E->RI->Out2.RegA = 0;
+                   } else {
+                       E->RI->Out.RegA = 0;
+                   }
+                   break;
+
+               case OP65_CMP:
+                   /* If this is an immidiate compare, the A register has
+                    * the value of the compare later.
+                    */
+                   if (CE_KnownImm (P)) {
+                       if (BC == BC_EQ) {
+                           E->RI->Out2.RegA = (unsigned char)P->Num;
+                       } else {
+                           E->RI->Out.RegA = (unsigned char)P->Num;
+                       }
+                   }
+                   break;
+
+               case OP65_CPX:
+                   /* If this is an immidiate compare, the X register has
+                    * the value of the compare later.
+                    */
+                   if (CE_KnownImm (P)) {
+                       if (BC == BC_EQ) {
+                           E->RI->Out2.RegX = (unsigned char)P->Num;
+                       } else {
+                           E->RI->Out.RegX = (unsigned char)P->Num;
+                       }
+                   }
+                   break;
+
+               case OP65_CPY:
+                   /* If this is an immidiate compare, the Y register has
+                    * the value of the compare later.
+                    */
+                   if (CE_KnownImm (P)) {
+                       if (BC == BC_EQ) {
+                           E->RI->Out2.RegY = (unsigned char)P->Num;
+                       } else {
+                           E->RI->Out.RegY = (unsigned char)P->Num;
+                       }
+                   }
+                   break;
+
+               case OP65_DEX:
+               case OP65_INX:
+               case OP65_LDX:
+               case OP65_PLX:
+                   /* X is zero in one execution flow direction */
+                   if (BC == BC_EQ) {
+                               E->RI->Out2.RegX = 0;
+                   } else {
+                       E->RI->Out.RegX = 0;
+                   }
+                   break;
+
+               case OP65_DEY:
+               case OP65_INY:
+               case OP65_LDY:
+               case OP65_PLY:
+                   /* X is zero in one execution flow direction */
+                   if (BC == BC_EQ) {
+                               E->RI->Out2.RegY = 0;
+                   } else {
+                       E->RI->Out.RegY = 0;
+                   }
+                   break;
+
+               case OP65_TAX:
+               case OP65_TXA:
+                   /* If the branch is a beq, both A and X are zero at the
+                    * branch target, otherwise they are zero at the next
+                    * insn.
+                    */
+                   if (BC == BC_EQ) {
+                       E->RI->Out2.RegA = E->RI->Out2.RegX = 0;
+                   } else {
+                       E->RI->Out.RegA = E->RI->Out.RegX = 0;
+                   }
+                   break;
+
+               case OP65_TAY:
+               case OP65_TYA:
+                   /* If the branch is a beq, both A and Y are zero at the
+                    * branch target, otherwise they are zero at the next
+                    * insn.
+                    */
+                   if (BC == BC_EQ) {
+                       E->RI->Out2.RegA = E->RI->Out2.RegY = 0;
+                   } else {
+                       E->RI->Out.RegA = E->RI->Out.RegY = 0;
+                   }
+                   break;
+
+               default:
+                   break;
 
+           }
+       }
+    }
+}