1 /*****************************************************************************/
5 /* Unnamed labels for the ca65 macroassembler */
9 /* (C) 2000 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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 /* Struct that describes an unnamed label */
56 typedef struct ULabel_ ULabel;
58 ULabel* Prev; /* Pointer to previous node in list */
59 ULabel* Next; /* Pointer to next node in list */
60 FilePos Pos; /* Position of the label in the source */
61 ExprNode* Val; /* The label value - may be NULL */
65 static ULabel* ULabRoot = 0; /* Root of the list */
66 static ULabel* ULabLast = 0; /* Last ULabel */
67 static ULabel* ULabLastDef = 0; /* Last defined ULabel */
68 static unsigned ULabCount = 0; /* Number of labels */
69 static unsigned ULabDefCount = 0; /* Number of defined labels */
70 static ULabel** ULabList = 0; /* Array with pointers to all labels */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static ULabel* NewULabel (ExprNode* Val)
81 /* Create a new ULabel and insert it into the list. The function will move
82 * ULabelLast, but not ULabelLastDef. The created label structure is returned.
85 /* Allocate memory for the ULabel structure */
86 ULabel* L = xmalloc (sizeof (ULabel));
88 /* Initialize the fields */
92 /* Insert the label into the list */
107 /* Return the created label */
113 ExprNode* ULabRef (int Which)
114 /* Get an unnamed label. If Which is negative, it is a backreference (a
115 * reference to an already defined label), and the function will return a
116 * segment relative expression. If Which is positive, it is a forward ref,
117 * and the function will return a expression node for an unnamed label that
118 * must be resolved later.
123 /* Which can never be 0 */
124 PRECONDITION (Which != 0);
126 /* Which is never really big (usually -3..+3), so a linear search is
127 * the best we can do here.
131 /* Backward reference */
132 while (Which < -1 && L != 0) {
137 /* Label does not exist */
138 Error (ERR_UNDEFINED_LABEL);
139 /* We must return something valid */
142 /* Return a copy of the label value */
143 return CloneExpr (L->Val);
146 /* Forward reference. Create labels as needed */
147 unsigned LabelNum = ULabDefCount + Which - 1;
155 /* Return an unnamed label expression */
156 return ULabelExpr (LabelNum);
163 /* Define an unnamed label at the current PC */
165 /* Create a new label if needed, or use an existing one */
166 if (ULabLastDef == 0 || ULabLastDef->Next == 0) {
167 /* The last label is also the last defined label, we need a new one */
168 ULabLastDef = NewULabel (CurrentPC ());
170 /* We do already have the label, but it's undefined until now */
171 ULabLastDef = ULabLastDef->Next;
172 ULabLastDef->Val = CurrentPC ();
173 ULabLastDef->Pos = CurPos;
180 int ULabCanResolve (void)
181 /* Return true if we can resolve arbitrary ULabels. */
183 /* We can resolve labels if we have built the necessary access array */
184 return (ULabList != 0);
189 ExprNode* ULabResolve (unsigned Index)
190 /* Return a valid expression for the unnamed label with the given index. This
191 * is used to resolve unnamed labels when assembly is done, so it is an error
192 * if a label is still undefined in this phase.
197 /* Must be in resolve phase and the index must be valid */
198 CHECK (ULabList != 0 && Index < ULabCount);
201 L = ULabList [Index];
203 /* If the label is open (not defined), return some valid value */
205 return LiteralExpr (0);
207 return CloneExpr (L->Val);
213 void ULabCheck (void)
214 /* Run through all unnamed labels and check for anomalies and errors */
218 /* Check if there are undefined labels */
220 L = ULabLastDef->Next;
222 PError (&L->Pos, ERR_UNDEFINED_LABEL);
227 /* Create an array that holds pointers to all labels. This allows us to
228 * access the labels quickly by index in the resolver phase at the end of
233 ULabList = xmalloc (ULabCount * sizeof (ULabel*));
240 CHECK (I == ULabCount);