+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 = CE_GetLabelCount (E);
while (LabelCount--) {
- CodeLabel* L = CE_GetLabel (E, LabelCount);
+ CodeLabel* L = CE_GetLabel (E, LabelCount);
L->Owner = 0;
CollAppend (&S->Labels, L);
}
/* Add an entry to the given code segment */
{
/* 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);
+ CS_MoveLabelsToEntry (S, E);
/* Add the entry to the list of code entries in this segment */
CollAppend (&S->Entries, E);
FreeCodeEntry (E);
}
-
+
void CS_DelEntries (CodeSeg* S, unsigned Start, unsigned Count)
/* Delete a range of code entries. This includes removing references to labels,
+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)
+ */
+{
+ /* 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));
+ }
+
+ /* Move the code block to the destination */
+ CollMoveMultiple (&S->Entries, Start, Count, NewPos);
+}
+
+
+
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.
* labels attached to the entries and so on.
*/
+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)
+ */
+
#if defined(HAVE_INLINE)
INLINE void CS_MoveEntry (CodeSeg* S, unsigned OldPos, unsigned NewPos)
/* Move an entry from one position to another. OldPos is the current position
# define CS_MoveEntry(S, OldPos, NewPos) CollMove (&(S)->Entries, OldPos, NewPos)
#endif
-#if defined(HAVE_INLINE)
-INLINE 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.
- */
-{
- CollMoveMultiple (&S->Entries, Start, Count, NewPos);
-}
-#else
-# define CS_MoveEntries(S, Start, Count, NewPos) CollMoveMultiple (&(S)->Entries, Start, Count, NewPos)
-#endif
-
#if defined(HAVE_INLINE)
INLINE struct CodeEntry* CS_GetEntry (CodeSeg* S, unsigned Index)
/* Get an entry from the given code segment */