]> git.sur5r.net Git - cc65/blob - src/ca65/symentry.h
ce342a83029a402f854d583b4b7cf82bdd4a2e38
[cc65] / src / ca65 / symentry.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                symentry.h                                 */
4 /*                                                                           */
5 /*          Symbol table entry forward for the ca65 macroassembler           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 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 #ifndef SYMENTRY_H
37 #define SYMENTRY_H
38
39
40
41 /* common */
42 #include "cddefs.h"
43 #include "coll.h"
44 #include "filepos.h"
45 #include "inline.h"
46
47 /* ca65 */
48 #include "spool.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Bits for the Flags value in SymEntry */
59 #define SF_NONE         0x0000          /* Empty flag set */
60 #define SF_USER         0x0001          /* User bit */
61 #define SF_UNUSED       0x0002          /* Unused entry */
62 #define SF_EXPORT       0x0004          /* Export this symbol */
63 #define SF_IMPORT       0x0008          /* Import this symbol */
64 #define SF_GLOBAL       0x0010          /* Global symbol */
65 #define SF_LABEL        0x0080          /* Used as a label */
66 #define SF_FORCED       0x0100          /* Forced import, SF_IMPORT also set */
67 #define SF_INDEXED      0x0800          /* Index is valid */
68 #define SF_MULTDEF      0x2000          /* Multiply defined symbol */
69 #define SF_DEFINED      0x4000          /* Defined */
70 #define SF_REFERENCED   0x8000          /* Referenced */
71
72 /* Arguments for SymFind... */
73 #define SYM_FIND_EXISTING       0
74 #define SYM_ALLOC_NEW           1
75
76 /* Structure of a symbol table entry */
77 typedef struct SymEntry SymEntry;
78 struct SymEntry {
79     SymEntry*               Left;       /* Lexically smaller entry */
80     SymEntry*               Right;      /* Lexically larger entry */
81     SymEntry*               List;       /* List of all entries */
82     SymEntry*               Locals;     /* Root of subtree for local symbols */
83     struct SymTable*        SymTab;     /* Table this symbol is in, 0 for locals */
84     FilePos                 Pos;        /* File position for this symbol */
85     unsigned                Flags;      /* Symbol flags */
86     unsigned                Index;      /* Index of import/export entries */
87     union {
88         struct ExprNode*    Expr;       /* Expression if CONST not set */
89         long                Val;        /* Value (if CONST set) */
90         SymEntry*           Sym;        /* Symbol (if trampoline entry) */
91     } V;
92     Collection              ExprRefs;   /* Expressions using this symbol */
93     unsigned char           ExportSize; /* Export address size */
94     unsigned char           AddrSize;   /* Address size of label */
95     unsigned char           ConDesPrio[CD_TYPE_COUNT];  /* ConDes priorities... */
96                                         /* ...actually value+1 (used as flag) */
97     unsigned                Name;       /* Name index in global string pool */
98 };
99
100 /* List of all symbol table entries */
101 extern SymEntry* SymList;
102
103 /* Pointer to last defined symbol */
104 extern SymEntry* SymLast;
105
106
107
108 /*****************************************************************************/
109 /*                                   Code                                    */
110 /*****************************************************************************/
111
112
113
114 int IsLocalName (const char* Name);
115 /* Return true if Name is the name of a local symbol */
116
117 int IsLocalNameId (unsigned Name);
118 /* Return true if Name is the name of a local symbol */
119
120 SymEntry* NewSymEntry (const char* Name);
121 /* Allocate a symbol table entry, initialize and return it */
122
123 int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E);
124 /* Search in the given tree for a name. If we find the symbol, the function
125  * will return 0 and put the entry pointer into E. If we did not find the
126  * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
127  * E will be set to the last entry, and the result of the function is <0 if
128  * the entry should be inserted on the left side, and >0 if it should get
129  * inserted on the right side.
130  */
131
132 #if defined(HAVE_INLINE)
133 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
134 /* Add an expression reference to this symbol */
135 {
136     CollAppend (&Sym->ExprRefs, Expr);
137 }
138 #else
139 #define SymAddExprRef(Sym,Expr)     CollAppend (&(Sym)->ExprRefs, Expr)
140 #endif
141
142 #if defined(HAVE_INLINE)
143 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
144 /* Delete an expression reference to this symbol */
145 {
146     CollDeleteItem (&Sym->ExprRefs, Expr);
147 }
148 #else
149 #define SymDelExprRef(Sym,Expr)     CollDeleteItem (&(Sym)->ExprRefs, Expr)
150 #endif
151
152 void SymTransferExprRefs (SymEntry* From, SymEntry* To);
153 /* Transfer all expression references from one symbol to another. */
154
155 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned char AddrSize, unsigned Flags);
156 /* Mark a symbol as defined */
157
158 void SymRef (SymEntry* Sym);
159 /* Mark the given symbol as referenced */
160
161 void SymImport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
162 /* Mark the given symbol as an imported symbol */
163
164 void SymExport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
165 /* Mark the given symbol as an exported symbol */
166
167 void SymGlobal (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
168 /* Mark the given symbol as a global symbol, that is, as a symbol that is
169  * either imported or exported.
170  */
171
172 void SymConDes (SymEntry* Sym, unsigned char AddrSize, unsigned Type, unsigned Prio);
173 /* Mark the given symbol as a module constructor/destructor. This will also
174  * mark the symbol as an export. Initializers may never be zero page symbols.
175  */
176
177 #if defined(HAVE_INLINE)
178 INLINE int SymIsDef (const SymEntry* S)
179 /* Return true if the given symbol is already defined */
180 {
181     return (S->Flags & SF_DEFINED) != 0;
182 }
183 #else
184 #  define SymIsDef(S)   (((S)->Flags & SF_DEFINED) != 0)
185 #endif
186
187 #if defined(HAVE_INLINE)
188 INLINE int SymIsRef (const SymEntry* S)
189 /* Return true if the given symbol has been referenced */
190 {
191     return (S->Flags & SF_REFERENCED) != 0;
192 }
193 #else
194 #  define SymIsRef(S)   (((S)->Flags & SF_REFERENCED) != 0)
195 #endif
196
197 #if defined(HAVE_INLINE)
198 INLINE int SymIsImport (const SymEntry* S)
199 /* Return true if the given symbol is marked as import */
200 {
201     /* Check the import flag */
202     return (S->Flags & SF_IMPORT) != 0;
203 }
204 #else
205 #  define SymIsImport(S)  (((S)->Flags & SF_IMPORT) != 0)
206 #endif
207
208 int SymIsConst (SymEntry* Sym, long* Val);
209 /* Return true if the given symbol has a constant value. If Val is not NULL
210  * and the symbol has a constant value, store it's value there.
211  */
212
213 #if defined(HAVE_INLINE)
214 INLINE int SymHasExpr (const SymEntry* S)
215 /* Return true if the given symbol has an associated expression */
216 {
217     /* Check the expression */
218     return ((S->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED);
219 }
220 #else
221 #  define SymHasExpr(S)   (((S)->Flags & (SF_DEFINED|SF_IMPORT)) != SF_DEFINED)
222 #endif
223
224 #if defined(HAVE_INLINE)
225 INLINE void SymMarkUser (SymEntry* S)
226 /* Set a user mark on the specified symbol */
227 {
228     /* Set the bit */
229     S->Flags |= SF_USER;
230 }
231 #else
232 #  define SymMarkUser(S)   ((S)->Flags |= SF_USER)
233 #endif
234
235 #if defined(HAVE_INLINE)
236 INLINE void SymUnmarkUser (SymEntry* S)
237 /* Remove a user mark from the specified symbol */
238 {
239     /* Reset the bit */
240     S->Flags &= ~SF_USER;
241 }
242 #else
243 #  define SymUnmarkUser(S)   ((S)->Flags &= ~SF_USER)
244 #endif
245
246 #if defined(HAVE_INLINE)
247 INLINE int SymHasUserMark (SymEntry* S)
248 /* Return the state of the user mark for the specified symbol */
249 {
250     /* Check the bit */
251     return (S->Flags & SF_USER) != 0;
252 }
253 #else
254 #  define SymHasUserMark(S) (((S)->Flags & SF_USER) != 0)
255 #endif
256
257 struct ExprNode* GetSymExpr (SymEntry* Sym);
258 /* Get the expression for a non-const symbol */
259
260 const struct ExprNode* SymResolve (const SymEntry* Sym);
261 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
262  * NULL. Do not call in other contexts!
263  */
264
265 #if defined(HAVE_INLINE)
266 INLINE const char* GetSymName (const SymEntry* S)
267 /* Return the name of the symbol */
268 {
269     return GetString (S->Name);
270 }
271 #else
272 #  define GetSymName(S)   GetString ((S)->Name)
273 #endif
274
275 #if defined(HAVE_INLINE)
276 INLINE unsigned char GetSymAddrSize (const SymEntry* S)
277 /* Return the address size of the symbol. Beware: This function will just
278  * return the AddrSize member, it will not look at the expression!
279  */
280 {
281     return S->AddrSize;
282 }
283 #else
284 #  define GetSymAddrSize(S)   ((S)->AddrSize)
285 #endif
286
287 long GetSymVal (SymEntry* Sym);
288 /* Return the value of a symbol assuming it's constant. FAIL will be called
289  * in case the symbol is undefined or not constant.
290  */
291
292 unsigned GetSymIndex (const SymEntry* Sym);
293 /* Return the symbol index for the given symbol */
294
295 #if defined(HAVE_INLINE)
296 INLINE const FilePos* GetSymPos (const SymEntry* S)
297 /* Return the position of first occurence in the source for the given symbol */
298 {
299     return &S->Pos;
300 }
301 #else
302 #  define GetSymPos(S)   (&(S)->Pos)
303 #endif
304
305
306
307 /* End of symentry.h */
308
309 #endif
310
311
312