]> git.sur5r.net Git - cc65/blob - src/da65/attrtab.c
d19b474fbbe4ad4dfb81580641ed47b3505739a2
[cc65] / src / da65 / attrtab.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 attrtab.c                                 */
4 /*                                                                           */
5 /*                       Disassembler attribute table                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 "error.h"
45 #include "attrtab.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Data                                    */
51 /*****************************************************************************/
52
53
54
55 /* Attribute table */
56 static unsigned char AttrTab [0x10000];
57
58 /* Symbol table */
59 static const char* SymTab [0x10000];
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 void AddrCheck (unsigned Addr)
70 /* Check if the given address has a valid range */
71 {
72     if (Addr >= 0x10000) {
73         Error ("Address out of range: %08X", Addr);
74     }
75 }
76
77
78
79 void MarkRange (unsigned Start, unsigned End, attr_t Attr)
80 /* Mark a range with the given attribute */
81 {
82     /* Do it easy here... */
83     while (Start <= End) {
84         MarkAddr (Start++, Attr);
85     }
86 }
87
88
89
90 void MarkAddr (unsigned Addr, attr_t Attr)
91 /* Mark an address with an attribute */
92 {
93     /* Check the given address */
94     AddrCheck (Addr);
95
96     /* We must not have more than one style bit */
97     if (Attr & atStyleMask) {
98         if (AttrTab[Addr] & atStyleMask) {
99             Error ("Duplicate style for address %04X", Addr);
100         }
101     }
102
103     /* Set the style */
104     AttrTab[Addr] |= Attr;
105 }
106
107
108
109 const char* MakeLabelName (unsigned Addr)
110 /* Make the default label name from the given address and return it in a
111  * static buffer.
112  */
113 {
114     static char LabelBuf [32];
115     xsprintf (LabelBuf, sizeof (LabelBuf), "L%04X", Addr);
116     return LabelBuf;
117 }
118
119
120
121 void AddLabel (unsigned Addr, const char* Name)
122 /* Add a label */
123 {
124     /* Check the given address */
125     AddrCheck (Addr);
126
127     /* Must not have two symbols for one address */
128     if (SymTab[Addr] != 0) {
129         if (strcmp (SymTab[Addr], Name) == 0) {
130             /* Allow label if it has the same name */
131             return;
132         }
133         Error ("Duplicate label for address %04X: %s/%s", Addr, SymTab[Addr], Name);
134     }
135
136     /* Create a new label */
137     SymTab[Addr] = xstrdup (Name);         
138 }
139
140
141
142 int HaveLabel (unsigned Addr)
143 /* Check if there is a label for the given address */
144 {
145     /* Check the given address */
146     AddrCheck (Addr);
147
148     /* Check for a label */
149     return (SymTab[Addr] != 0);
150 }
151
152
153
154 const char* GetLabel (unsigned Addr)
155 /* Return the label for an address */
156 {
157     /* Check the given address */
158     AddrCheck (Addr);
159
160     /* Return the label if any */
161     return SymTab[Addr];
162 }
163
164
165
166 unsigned char GetStyle (unsigned Addr)
167 /* Return the style attribute for the given address */
168 {
169     /* Check the given address */
170     AddrCheck (Addr);
171
172     /* Return the attribute */
173     return (AttrTab[Addr] & atStyleMask);
174 }
175
176
177