]> git.sur5r.net Git - cc65/blob - src/da65/attrtab.c
Allow conditional directives within .STRUCT7:UNION and .ENUM
[cc65] / src / da65 / attrtab.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 attrtab.c                                 */
4 /*                                                                           */
5 /*                       Disassembler attribute table                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 #include <stdio.h>
37 #include <string.h>
38
39 /* common */
40 #include "xmalloc.h"
41 #include "xsprintf.h"
42
43 /* da65 */
44 #include "code.h"
45 #include "error.h"
46 #include "global.h"
47 #include "output.h"
48 #include "attrtab.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Attribute table */
59 static unsigned char AttrTab [0x10000];
60
61 /* Symbol table */
62 static const char* SymTab [0x10000];
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static void AddrCheck (unsigned Addr)
73 /* Check if the given address has a valid range */
74 {
75     if (Addr >= 0x10000) {
76         Error ("Address out of range: %08X", Addr);
77     }
78 }
79
80
81
82 void MarkRange (unsigned Start, unsigned End, attr_t Attr)
83 /* Mark a range with the given attribute */
84 {
85     /* Do it easy here... */
86     while (Start <= End) {
87         MarkAddr (Start++, Attr);
88     }
89 }
90
91
92
93 void MarkAddr (unsigned Addr, attr_t Attr)
94 /* Mark an address with an attribute */
95 {
96     /* Check the given address */
97     AddrCheck (Addr);
98
99     /* We must not have more than one style bit */
100     if (Attr & atStyleMask) {
101         if (AttrTab[Addr] & atStyleMask) {
102             Error ("Duplicate style for address %04X", Addr);
103         }
104     }
105
106     /* Set the style */
107     AttrTab[Addr] |= Attr;
108 }
109
110
111
112 const char* MakeLabelName (unsigned Addr)
113 /* Make the default label name from the given address and return it in a
114  * static buffer.
115  */
116 {
117     static char LabelBuf [32];
118     xsprintf (LabelBuf, sizeof (LabelBuf), "L%04X", Addr);
119     return LabelBuf;
120 }
121
122
123
124 void AddLabel (unsigned Addr, attr_t Attr, const char* Name)
125 /* Add a label */
126 {
127     /* Get an existing label attribute */
128     attr_t ExistingAttr = GetLabelAttr (Addr);
129
130     /* Must not have two symbols for one address */
131     if (ExistingAttr != atNoLabel) {
132         /* Allow redefinition if identical */
133         if (ExistingAttr == Attr && strcmp (SymTab[Addr], Name) == 0) {
134             return;
135         }
136         Error ("Duplicate label for address %04X: %s/%s", Addr, SymTab[Addr], Name);
137     }
138
139     /* Create a new label */
140     SymTab[Addr] = xstrdup (Name);
141
142     /* Remember the attribute */
143     AttrTab[Addr] |= Attr;
144 }
145
146
147
148 void AddExtLabelRange (unsigned Addr, const char* Name, unsigned Count)
149 /* Add an external label for a range. The first entry gets the label "Name"
150  * while the others get "Name+offs".
151  */
152 {
153     /* Define the label */
154     AddLabel (Addr, atExtLabel, Name);
155
156     /* Define dependent labels if necessary */
157     if (Count > 1) {
158         unsigned Offs;
159
160         /* Setup the format string */
161         const char* Format = UseHexOffs? "$%02X" : "%u";
162
163         /* Allocate memory for the dependent label names */
164         unsigned NameLen = strlen (Name);
165         char*    DepName = xmalloc (NameLen + 7);
166         char*    DepOffs = DepName + NameLen + 1;
167
168         /* Copy the original name into the buffer */
169         memcpy (DepName, Name, NameLen);
170         DepName[NameLen] = '+';
171
172         /* Define the labels */
173         for (Offs = 1; Offs < Count; ++Offs) {
174             sprintf (DepOffs, Format, Offs);
175             AddLabel (Addr + Offs, atDepLabel, DepName);
176         }
177
178         /* Free the name buffer */
179         xfree (DepName);
180     }
181 }
182
183
184
185 int HaveLabel (unsigned Addr)
186 /* Check if there is a label for the given address */
187 {
188     /* Check the given address */
189     AddrCheck (Addr);
190
191     /* Check for a label */
192     return (SymTab[Addr] != 0);
193 }
194
195
196
197 int MustDefLabel (unsigned Addr)
198 /* Return true if we must define a label for this address, that is, if there
199  * is a label at this address, and it is an external or internal label.
200  */
201 {
202     /* Get the label attribute */
203     attr_t A = GetLabelAttr (Addr);
204
205     /* Check for an internal or external label */
206     return (A == atExtLabel || A == atIntLabel);
207 }
208
209
210
211 const char* GetLabel (unsigned Addr)
212 /* Return the label for an address */
213 {
214     /* Check the given address */
215     AddrCheck (Addr);
216
217     /* Return the label if any */
218     return SymTab[Addr];
219 }
220
221
222
223 unsigned char GetStyleAttr (unsigned Addr)
224 /* Return the style attribute for the given address */
225 {
226     /* Check the given address */
227     AddrCheck (Addr);
228
229     /* Return the attribute */
230     return (AttrTab[Addr] & atStyleMask);
231 }
232
233
234
235 unsigned char GetLabelAttr (unsigned Addr)
236 /* Return the label attribute for the given address */
237 {
238     /* Check the given address */
239     AddrCheck (Addr);
240
241     /* Return the attribute */
242     return (AttrTab[Addr] & atLabelMask);
243 }
244
245
246
247 static void DefineConst (unsigned Addr)
248 /* Define an address constant */
249 {
250     Output ("%s", SymTab [Addr]);
251     Indent (AIndent);
252     Output ("= $%04X", Addr);
253     LineFeed ();
254 }
255
256
257
258 void DefOutOfRangeLabels (void)
259 /* Output any labels that are out of the loaded code range */
260 {
261     unsigned long Addr;
262
263     SeparatorLine ();
264
265     /* Low range */
266     Addr = 0;
267     while (Addr < CodeStart) {
268         if (MustDefLabel (Addr)) {
269             DefineConst (Addr);
270         }
271         ++Addr;
272     }
273
274     /* Skip areas in code range */
275     while (Addr <= CodeEnd) {
276         if ((AttrTab[Addr] & atStyleMask) == atSkip && MustDefLabel (Addr)) {
277             DefineConst (Addr);
278         }
279         ++Addr;
280     }
281
282     /* High range */
283     while (Addr < 0x10000) {
284         if (MustDefLabel (Addr)) {
285             DefineConst (Addr);
286         }
287         ++Addr;
288     }
289
290     SeparatorLine ();
291 }
292
293
294