1 /*****************************************************************************/
5 /* Code label structure */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 CodeLabel* NewCodeLabel (const char* Name, unsigned Hash)
53 /* Create a new code label, initialize and return it */
56 CodeLabel* L = xmalloc (sizeof (CodeLabel));
58 /* Initialize the fields */
60 L->Name = xstrdup (Name);
64 InitCollection (&L->JumpFrom);
66 /* Return the new label */
72 void FreeCodeLabel (CodeLabel* L)
73 /* Free the given code label */
78 /* Free the collection */
79 DoneCollection (&L->JumpFrom);
81 /* Delete the struct */
87 void AddLabelRef (CodeLabel* L, struct CodeEntry* E)
88 /* Let the CodeEntry E reference the label L */
90 /* The insn at E jumps to this label */
93 /* Remember that in the label */
94 CollAppend (&L->JumpFrom, E);
99 unsigned RemoveLabelRef (CodeLabel* L, const struct CodeEntry* E)
100 /* Remove a reference to this label, return the number of remaining references */
102 /* Delete the item */
103 CollDeleteItem (&L->JumpFrom, E);
105 /* Return the number of remaining references */
106 return CollCount (&L->JumpFrom);
111 void MoveLabelRefs (CodeLabel* OldLabel, CodeLabel* NewLabel)
112 /* Move all references to OldLabel to point to NewLabel. OldLabel will have no
113 * more references on return.
116 /* Walk through all instructions referencing the old label */
117 unsigned Count = CollCount (&OldLabel->JumpFrom);
120 /* Get the instruction that references the old label */
121 CodeEntry* E = CollAt (&OldLabel->JumpFrom, Count);
123 /* Change the reference to the new label */
124 CHECK (E->JumpTo == OldLabel);
125 AddLabelRef (NewLabel, E);
129 /* There are no more references to the old label */
130 CollDeleteAll (&OldLabel->JumpFrom);
135 void OutputCodeLabel (const CodeLabel* L, FILE* F)
136 /* Output the code label to a file */
138 fprintf (F, "%s:", L->Name);