1 /*****************************************************************************/
5 /* Disassembler attribute table */
9 /* (C) 2000-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
59 static unsigned char AttrTab [0x10000];
62 static const char* SymTab [0x10000];
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void AddrCheck (unsigned Addr)
73 /* Check if the given address has a valid range */
75 if (Addr >= 0x10000) {
76 Error ("Address out of range: %08X", Addr);
82 void MarkRange (unsigned Start, unsigned End, attr_t Attr)
83 /* Mark a range with the given attribute */
85 /* Do it easy here... */
86 while (Start <= End) {
87 MarkAddr (Start++, Attr);
93 void MarkAddr (unsigned Addr, attr_t Attr)
94 /* Mark an address with an attribute */
96 /* Check the given address */
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);
107 AttrTab[Addr] |= Attr;
112 const char* MakeLabelName (unsigned Addr)
113 /* Make the default label name from the given address and return it in a
117 static char LabelBuf [32];
118 xsprintf (LabelBuf, sizeof (LabelBuf), "L%04X", Addr);
124 void AddLabel (unsigned Addr, attr_t Attr, const char* Name)
127 /* Get an existing label attribute */
128 attr_t ExistingAttr = GetLabelAttr (Addr);
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) {
136 Error ("Duplicate label for address %04X: %s/%s", Addr, SymTab[Addr], Name);
139 /* Create a new label */
140 SymTab[Addr] = xstrdup (Name);
142 /* Remember the attribute */
143 AttrTab[Addr] |= Attr;
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".
153 /* Define the label */
154 AddLabel (Addr, atExtLabel, Name);
156 /* Define dependent labels if necessary */
160 /* Allocate memory for the dependent label names */
161 unsigned NameLen = strlen (Name);
162 char* DepName = xmalloc (NameLen + 7);
163 char* DepOffs = DepName + NameLen + 1;
165 /* Copy the original name into the buffer */
166 memcpy (DepName, Name, NameLen);
167 DepName[NameLen] = '+';
169 /* Define the labels */
170 for (Offs = 1; Offs < Count; ++Offs) {
171 sprintf (DepOffs, "%u", Offs);
172 AddLabel (Addr + Offs, atDepLabel, DepName);
175 /* Free the name buffer */
182 int HaveLabel (unsigned Addr)
183 /* Check if there is a label for the given address */
185 /* Check the given address */
188 /* Check for a label */
189 return (SymTab[Addr] != 0);
194 int MustDefLabel (unsigned Addr)
195 /* Return true if we must define a label for this address, that is, if there
196 * is a label at this address, and it is an external or internal label.
199 /* Get the label attribute */
200 attr_t A = GetLabelAttr (Addr);
202 /* Check for an internal or external label */
203 return (A == atExtLabel || A == atIntLabel);
208 const char* GetLabel (unsigned Addr)
209 /* Return the label for an address */
211 /* Check the given address */
214 /* Return the label if any */
220 unsigned char GetStyleAttr (unsigned Addr)
221 /* Return the style attribute for the given address */
223 /* Check the given address */
226 /* Return the attribute */
227 return (AttrTab[Addr] & atStyleMask);
232 unsigned char GetLabelAttr (unsigned Addr)
233 /* Return the label attribute for the given address */
235 /* Check the given address */
238 /* Return the attribute */
239 return (AttrTab[Addr] & atLabelMask);
244 static void DefineConst (unsigned Addr)
245 /* Define an address constant */
247 Output ("%s", SymTab [Addr]);
249 Output ("= $%04X", Addr);
255 void DefOutOfRangeLabels (void)
256 /* Output any labels that are out of the loaded code range */
263 for (Addr = 0; Addr < CodeStart; ++Addr) {
264 if (MustDefLabel (Addr)) {
270 for (Addr = CodeEnd+1; Addr < 0x10000; ++Addr) {
271 if (MustDefLabel (Addr)) {