+ } else {
+
+ /* The new entry does not have a label, just move them */
+ while (OldLabelCount--) {
+
+ /* Move the label to the new entry */
+ CE_MoveLabel (CE_GetLabel (Old, OldLabelCount), New);
+
+ }
+
+ }
+}
+
+
+
+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
+ * deleted.
+ */
+{
+ /* Get a pointer to the label and make sure it exists */
+ CodeLabel* L = E->JumpTo;
+ CHECK (L != 0);
+
+ /* 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) {
+ CS_DelLabel (S, 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.
+ */
+{
+ /* Get the old label */
+ CodeLabel* OldLabel = E->JumpTo;
+
+ /* Be sure that code entry references a label */
+ PRECONDITION (OldLabel != 0);
+
+ /* Remove the reference to our label */
+ CS_RemoveLabelRef (S, E);
+
+ /* Use the new label */
+ CL_AddRef (L, E);
+}
+
+
+
+void CS_DelCodeAfter (CodeSeg* S, unsigned Last)
+/* Delete all entries including the given one */
+{
+ /* Get the number of entries in this segment */
+ unsigned Count = CS_GetEntryCount (S);
+
+ /* 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 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 */
+ CS_RemoveLabelRef (S, 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 = CS_GetEntry (S, C);
+
+ /* Check if this entry has a label attached */
+ if (CE_HasLabel (E)) {
+ /* Move the labels to the pool and clear the owner pointer */
+ CS_MoveLabelsToPool (S, E);
+ }
+
+ /* Delete the pointer to the entry */
+ CollDelete (&S->Entries, C);
+
+ /* Delete the entry itself */
+ FreeCodeEntry (E);
+ }
+}
+
+
+
+void CS_ResetMarks (CodeSeg* S, unsigned First, unsigned Last)
+/* Remove all user marks from the entries in the given range */
+{
+ while (First <= Last) {
+ CE_ResetMark (CS_GetEntry (S, First++));
+ }
+}
+
+
+
+int CS_IsBasicBlock (CodeSeg* S, unsigned First, unsigned Last)
+/* Check if the given code segment range is a basic block. That is, check if
+ * First is the only entrance and Last is the only exit. This means that no
+ * jump/branch inside the block may jump to an insn below First or after(!)
+ * Last, and that no insn may jump into this block from the outside.
+ */
+{
+ unsigned I;
+
+ /* Don't accept invalid ranges */
+ CHECK (First <= Last);
+
+ /* First pass: Walk over the range and remove all marks from the entries */
+ CS_ResetMarks (S, First, Last);
+
+ /* Second pass: Walk over the range checking all labels. Note: There may be
+ * label on the first insn which is ok.
+ */
+ I = First + 1;
+ while (I <= Last) {
+
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if this entry has one or more labels, if so, check which
+ * entries jump to this label.
+ */
+ unsigned LabelCount = CE_GetLabelCount (E);
+ unsigned LabelIndex;
+ for (LabelIndex = 0; LabelIndex < LabelCount; ++LabelIndex) {
+
+ /* Get this label */
+ CodeLabel* L = CE_GetLabel (E, LabelIndex);
+
+ /* Walk over all entries that jump to this label. Check for each
+ * of the entries if it is out of the range.
+ */
+ unsigned RefCount = CL_GetRefCount (L);
+ unsigned RefIndex;
+ for (RefIndex = 0; RefIndex < RefCount; ++RefIndex) {
+
+ /* Get the code entry that jumps here */
+ CodeEntry* Ref = CL_GetRef (L, RefIndex);
+
+ /* Walk over out complete range and check if we find the
+ * refering entry. This is cheaper than using CS_GetEntryIndex,
+ * because CS_GetEntryIndex will search the complete code
+ * segment and not just our range.
+ */
+ unsigned J;
+ for (J = First; J <= Last; ++J) {
+ if (Ref == CS_GetEntry (S, J)) {
+ break;
+ }
+ }
+ if (J > Last) {
+ /* We did not find the entry. This means that the jump to
+ * out code segment entry E came from outside the range,
+ * which in turn means that the given range is not a basic
+ * block.
+ */
+ CS_ResetMarks (S, First, Last);
+ return 0;
+ }
+
+ /* If we come here, we found the entry. Mark it, so we know
+ * that the branch to the label is in range.
+ */
+ CE_SetMark (Ref);
+ }
+ }
+
+ /* Next entry */
+ ++I;
+ }
+
+ /* Third pass: Walk again over the range and check all branches. If we
+ * find a branch that is not marked, its target is not inside the range
+ * (since we checked all the labels in the range before).
+ */
+ I = First;
+ while (I <= Last) {
+
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if this is a branch and if so, if it has a mark */
+ if (E->Info & (OF_UBRA | OF_CBRA)) {
+ if (!CE_HasMark (E)) {
+ /* No mark means not a basic block. Before bailing out, be sure
+ * to remove the marks from the remaining entries.
+ */
+ CS_ResetMarks (S, I+1, Last);
+ return 0;
+ }
+
+ /* Remove the mark */
+ CE_ResetMark (E);
+ }
+
+ /* Next entry */
+ ++I;
+ }
+
+ /* Done - this is a basic block */
+ return 1;
+}
+
+
+
+void CS_OutputPrologue (const CodeSeg* S, FILE* F)
+/* If the given code segment is a code segment for a function, output the
+ * assembler prologue into the file. That is: Output a comment header, switch
+ * to the correct segment and enter the local function scope. If the code
+ * segment is global, do nothing.
+ */
+{
+ /* Get the function associated with the code segment */
+ SymEntry* Func = S->Func;
+
+ /* If the code segment is associated with a function, print a function
+ * header and enter a local scope. Be sure to switch to the correct
+ * segment before outputing the function label.
+ */
+ if (Func) {
+ CS_PrintFunctionHeader (S, F);
+ fprintf (F, ".segment\t\"%s\"\n\n.proc\t_%s\n\n", S->SegName, Func->Name);
+ }
+
+}
+
+
+
+void CS_OutputEpilogue (const CodeSeg* S, FILE* F)
+/* If the given code segment is a code segment for a function, output the
+ * assembler epilogue into the file. That is: Close the local function scope.
+ */
+{
+ if (S->Func) {
+ fprintf (F, "\n.endproc\n\n");
+ }
+}
+
+
+
+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 = CS_GetEntryCount (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);
+
+ /* 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.