]> git.sur5r.net Git - cc65/blob - src/ca65/ulabel.c
Added search paths similar to that of the linker and compiler.
[cc65] / src / ca65 / ulabel.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 ulabel.c                                  */
4 /*                                                                           */
5 /*                Unnamed labels for the ca65 macroassembler                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2004 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 52                                              */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "check.h"
38 #include "coll.h"
39 #include "filepos.h"
40 #include "xmalloc.h"
41
42 /* ca65 */
43 #include "error.h"
44 #include "expr.h"
45 #include "scanner.h"
46 #include "ulabel.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 /* Struct that describes an unnamed label */
57 typedef struct ULabel ULabel;
58 struct ULabel {
59     FilePos     Pos;                    /* Position of the label in the source */
60     ExprNode*   Val;                    /* The label value - may be NULL */
61     unsigned    Ref;                    /* Number of references */
62 };
63
64 /* List management */
65 static Collection ULabList      = STATIC_COLLECTION_INITIALIZER;
66 static unsigned ULabDefCount    = 0;    /* Number of defined labels */
67
68
69
70 /*****************************************************************************/
71 /*                                   Code                                    */
72 /*****************************************************************************/
73
74
75
76 static ULabel* NewULabel (ExprNode* Val)
77 /* Create a new ULabel and insert it into the collection. The created label
78  * structure is returned.
79  */
80 {
81     /* Allocate memory for the ULabel structure */
82     ULabel* L = xmalloc (sizeof (ULabel));
83
84     /* Initialize the fields */
85     L->Pos = CurPos;
86     L->Val = Val;
87     L->Ref = 0;
88
89     /* Insert the label into the collection */
90     CollAppend (&ULabList, L);
91
92     /* Return the created label */
93     return L;
94 }
95
96
97
98 ExprNode* ULabRef (int Which)
99 /* Get an unnamed label. If Which is negative, it is a backreference (a
100  * reference to an already defined label), and the function will return a
101  * segment relative expression. If Which is positive, it is a forward ref,
102  * and the function will return a expression node for an unnamed label that
103  * must be resolved later.
104  */
105 {
106     int     Index;
107     ULabel* L;
108
109     /* Which can never be 0 */
110     PRECONDITION (Which != 0);
111
112     /* Get the index of the referenced label */
113     if (Which > 0) {
114         --Which;
115     }
116     Index = (int) ULabDefCount + Which;
117
118     /* We cannot have negative label indices */
119     if (Index < 0) {
120         /* Label does not exist */
121         Error ("Undefined label");
122         /* We must return something valid */
123         return GenCurrentPC();
124     }
125
126     /* Check if the label exists. If not, generate enough forward labels. */
127     if (Index < (int) CollCount (&ULabList)) {
128         /* The label exists, get it. */
129         L = CollAtUnchecked (&ULabList, Index);
130     } else {
131         /* Generate new, undefined labels */
132         while (Index >= (int) CollCount (&ULabList)) {
133             L = NewULabel (0);
134         }
135     }
136
137     /* Mark the label as referenced */
138     ++L->Ref;
139
140     /* If the label is already defined, return its value, otherwise return
141      * just a reference.
142      */
143     if (L->Val) {
144         return CloneExpr (L->Val);
145     } else {
146         return GenULabelExpr (Index);
147     }
148 }
149
150
151
152 void ULabDef (void)
153 /* Define an unnamed label at the current PC */
154 {
155     if (ULabDefCount < CollCount (&ULabList)) {
156         /* We did already have a forward reference to this label, so has
157          * already been generated, but doesn't have a value. Use the current
158          * PC for the label value.
159          */
160         ULabel* L = CollAtUnchecked (&ULabList, ULabDefCount);
161         CHECK (L->Val == 0);
162         L->Val = GenCurrentPC ();
163         L->Pos = CurPos;
164     } else {
165         /* There is no such label, create it */
166         NewULabel (GenCurrentPC ());
167     }
168
169     /* We have one more defined label */
170     ++ULabDefCount;
171 }
172
173
174
175 int ULabCanResolve (void)
176 /* Return true if we can resolve arbitrary ULabels. */
177 {
178     /* We can resolve labels if we don't have any undefineds */
179     return (ULabDefCount == CollCount (&ULabList));
180 }
181
182
183
184 ExprNode* ULabResolve (unsigned Index)
185 /* Return a valid expression for the unnamed label with the given index. This
186  * is used to resolve unnamed labels when assembly is done, so it is an error
187  * if a label is still undefined in this phase.
188  */
189 {
190     /* Get the label and check that it is defined */
191     ULabel* L = CollAt (&ULabList, Index);
192     CHECK (L->Val != 0);
193
194     /* Return the label value */
195     return CloneExpr (L->Val);
196 }
197
198
199
200 void ULabCheck (void)
201 /* Run through all unnamed labels and check for anomalies and errors */
202 {
203     /* Check if there are undefined labels */
204     unsigned I = ULabDefCount;
205     while (I < CollCount (&ULabList)) {
206         ULabel* L = CollAtUnchecked (&ULabList, I);
207         PError (&L->Pos, "Undefined label");
208         ++I;
209     }
210
211     /* Walk over all labels and emit a warning if any unreferenced ones
212      * are found.
213      */
214     for (I = 0; I < CollCount (&ULabList); ++I) {
215         ULabel* L = CollAtUnchecked (&ULabList, I);
216         if (L->Ref == 0) {
217             PWarning (&L->Pos, 1, "No reference to unnamed label");
218         }
219     }
220 }
221
222
223