]> git.sur5r.net Git - cc65/blob - src/ca65/symentry.h
Fixed a problem reported by thefox: A symbol reference with an explicit scope
[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-2012, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 #include "strbuf.h"
47
48 /* ca65 */
49 #include "spool.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Forwards */
60 struct HLLDbgSym;
61
62 /* Bits for the Flags value in SymEntry */
63 #define SF_NONE         0x0000          /* Empty flag set */
64 #define SF_USER         0x0001          /* User bit */
65 #define SF_UNUSED       0x0002          /* Unused entry */
66 #define SF_EXPORT       0x0004          /* Export this symbol */
67 #define SF_IMPORT       0x0008          /* Import this symbol */
68 #define SF_GLOBAL       0x0010          /* Global symbol */
69 #define SF_LOCAL        0x0020          /* Cheap local symbol */
70 #define SF_LABEL        0x0040          /* Used as a label */
71 #define SF_VAR          0x0080          /* Variable symbol */
72 #define SF_FORCED       0x0100          /* Forced import, SF_IMPORT also set */
73 #define SF_FIXED        0x0200          /* May not be trampoline */
74 #define SF_MULTDEF      0x1000          /* Multiply defined symbol */
75 #define SF_DEFINED      0x2000          /* Defined */
76 #define SF_REFERENCED   0x4000          /* Referenced */
77
78 /* Combined values */
79 #define SF_REFIMP       (SF_REFERENCED|SF_IMPORT)       /* A ref'd import */
80
81 /* Arguments for SymFind... */
82 #define SYM_FIND_EXISTING       0
83 #define SYM_ALLOC_NEW           1
84
85 /* Structure of a symbol table entry */
86 typedef struct SymEntry SymEntry;
87 struct SymEntry {
88     SymEntry*           Left;           /* Lexically smaller entry */
89     SymEntry*           Right;          /* Lexically larger entry */
90     SymEntry*           List;           /* List of all entries */
91     SymEntry*           Locals;         /* Root of subtree for local symbols */
92     union {
93         struct SymTable*    Tab;        /* Table this symbol is in */
94         struct SymEntry*    Entry;      /* Parent for cheap locals */
95     } Sym;
96     Collection          DefLines;       /* Line infos for definition */
97     Collection          RefLines;       /* Line infos for references */
98     FilePos*            GuessedUse[1];  /* File position where symbol
99                                          * address size was guessed, and the
100                                          * smallest possible addressing was NOT
101                                          * used. Currently only for zero page
102                                          * addressing
103                                          */
104     struct HLLDbgSym*   HLLSym;         /* Symbol from high level language */
105     unsigned            Flags;          /* Symbol flags */
106     unsigned            DebugSymId;     /* Debug symbol id */
107     unsigned            ImportId;       /* Id of import if this is one */
108     unsigned            ExportId;       /* Id of export if this is one */
109     struct ExprNode*    Expr;           /* Symbol expression */
110     Collection          ExprRefs;       /* Expressions using this symbol */
111     unsigned char       ExportSize;     /* Export address size */
112     unsigned char       AddrSize;       /* Address size of label */
113     unsigned char       ConDesPrio[CD_TYPE_COUNT];      /* ConDes priorities... */
114                                         /* ...actually value+1 (used as flag) */
115     unsigned            Name;           /* Name index in global string pool */
116 };
117
118 /* List of all symbol table entries */
119 extern SymEntry* SymList;
120
121 /* Pointer to last defined symbol */
122 extern SymEntry* SymLast;
123
124
125
126 /*****************************************************************************/
127 /*                                   Code                                    */
128 /*****************************************************************************/
129
130
131
132 SymEntry* NewSymEntry (const StrBuf* Name, unsigned Flags);
133 /* Allocate a symbol table entry, initialize and return it */
134
135 int SymSearchTree (SymEntry* T, const StrBuf* Name, SymEntry** E);
136 /* Search in the given tree for a name. If we find the symbol, the function
137  * will return 0 and put the entry pointer into E. If we did not find the
138  * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
139  * E will be set to the last entry, and the result of the function is <0 if
140  * the entry should be inserted on the left side, and >0 if it should get
141  * inserted on the right side.
142  */
143
144 #if defined(HAVE_INLINE)
145 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
146 /* Add an expression reference to this symbol */
147 {
148     CollAppend (&Sym->ExprRefs, Expr);
149 }
150 #else
151 #define SymAddExprRef(Sym,Expr)     CollAppend (&(Sym)->ExprRefs, Expr)
152 #endif
153
154 #if defined(HAVE_INLINE)
155 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
156 /* Delete an expression reference to this symbol */
157 {
158     CollDeleteItem (&Sym->ExprRefs, Expr);
159 }
160 #else
161 #define SymDelExprRef(Sym,Expr)     CollDeleteItem (&(Sym)->ExprRefs, Expr)
162 #endif
163
164 void SymTransferExprRefs (SymEntry* From, SymEntry* To);
165 /* Transfer all expression references from one symbol to another. */
166
167 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned char AddrSize, unsigned Flags);
168 /* Mark a symbol as defined */
169
170 void SymRef (SymEntry* Sym);
171 /* Mark the given symbol as referenced */
172
173 void SymImport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
174 /* Mark the given symbol as an imported symbol */
175
176 void SymExport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
177 /* Mark the given symbol as an exported symbol */
178
179 void SymGlobal (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
180 /* Mark the given symbol as a global symbol, that is, as a symbol that is
181  * either imported or exported.
182  */
183
184 void SymConDes (SymEntry* Sym, unsigned char AddrSize, unsigned Type, unsigned Prio);
185 /* Mark the given symbol as a module constructor/destructor. This will also
186  * mark the symbol as an export. Initializers may never be zero page symbols.
187  */
188
189 void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize);
190 /* Mark the address size of the given symbol as guessed. The address size
191  * passed as argument is the one NOT used, because the actual address size
192  * wasn't known. Example: Zero page addressing was not used because symbol
193  * is undefined, and absolute addressing was available.
194  */
195
196 void SymExportFromGlobal (SymEntry* S);
197 /* Called at the end of assembly. Converts a global symbol that is defined
198  * into an export.
199  */
200
201 void SymImportFromGlobal (SymEntry* S);
202 /* Called at the end of assembly. Converts a global symbol that is undefined
203  * into an import.
204  */
205
206 #if defined(HAVE_INLINE)
207 INLINE int SymIsDef (const SymEntry* S)
208 /* Return true if the given symbol is already defined */
209 {
210     return (S->Flags & SF_DEFINED) != 0;
211 }
212 #else
213 #  define SymIsDef(S)   (((S)->Flags & SF_DEFINED) != 0)
214 #endif
215
216 #if defined(HAVE_INLINE)
217 INLINE int SymIsRef (const SymEntry* S)
218 /* Return true if the given symbol has been referenced */
219 {
220     return (S->Flags & SF_REFERENCED) != 0;
221 }
222 #else
223 #  define SymIsRef(S)   (((S)->Flags & SF_REFERENCED) != 0)
224 #endif
225
226 #if defined(HAVE_INLINE)
227 INLINE int SymIsImport (const SymEntry* S)
228 /* Return true if the given symbol is marked as import */
229 {
230     /* Check the import flag */
231     return (S->Flags & SF_IMPORT) != 0;
232 }
233 #else
234 #  define SymIsImport(S)  (((S)->Flags & SF_IMPORT) != 0)
235 #endif
236
237 #if defined(HAVE_INLINE)
238 INLINE int SymIsExport (const SymEntry* S)
239 /* Return true if the given symbol is marked as export */
240 {
241     /* Check the export flag */
242     return (S->Flags & SF_EXPORT) != 0;
243 }
244 #else
245 #  define SymIsExport(S)  (((S)->Flags & SF_EXPORT) != 0)
246 #endif
247
248 #if defined(HAVE_INLINE)
249 INLINE int SymIsVar (const SymEntry* S)
250 /* Return true if the given symbol is marked as variable */
251 {
252     /* Check the variable flag */
253     return (S->Flags & SF_VAR) != 0;
254 }
255 #else
256 #  define SymIsVar(S)   (((S)->Flags & SF_VAR) != 0)
257 #endif
258
259 int SymIsConst (const SymEntry* Sym, long* Val);
260 /* Return true if the given symbol has a constant value. If Val is not NULL
261  * and the symbol has a constant value, store it's value there.
262  */
263
264 #if defined(HAVE_INLINE)
265 INLINE int SymHasExpr (const SymEntry* S)
266 /* Return true if the given symbol has an associated expression */
267 {
268     /* Check the expression */
269     return ((S->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED);
270 }
271 #else
272 #  define SymHasExpr(S)   (((S)->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED)
273 #endif
274
275 #if defined(HAVE_INLINE)
276 INLINE void SymMarkUser (SymEntry* S)
277 /* Set a user mark on the specified symbol */
278 {
279     /* Set the bit */
280     S->Flags |= SF_USER;
281 }
282 #else
283 #  define SymMarkUser(S)   ((S)->Flags |= SF_USER)
284 #endif
285
286 #if defined(HAVE_INLINE)
287 INLINE void SymUnmarkUser (SymEntry* S)
288 /* Remove a user mark from the specified symbol */
289 {
290     /* Reset the bit */
291     S->Flags &= ~SF_USER;
292 }
293 #else
294 #  define SymUnmarkUser(S)   ((S)->Flags &= ~SF_USER)
295 #endif
296
297 #if defined(HAVE_INLINE)
298 INLINE int SymHasUserMark (SymEntry* S)
299 /* Return the state of the user mark for the specified symbol */
300 {
301     /* Check the bit */
302     return (S->Flags & SF_USER) != 0;
303 }
304 #else
305 #  define SymHasUserMark(S) (((S)->Flags & SF_USER) != 0)
306 #endif
307
308 struct SymTable* GetSymParentScope (SymEntry* S);
309 /* Get the parent scope of the symbol (not the one it is defined in). Return
310  * NULL if the symbol is a cheap local, or defined on global level.
311  */
312
313 struct ExprNode* GetSymExpr (SymEntry* Sym);
314 /* Get the expression for a non-const symbol */
315
316 const struct ExprNode* SymResolve (const SymEntry* Sym);
317 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
318  * NULL. Do not call in other contexts!
319  */
320
321 #if defined(HAVE_INLINE)
322 INLINE const StrBuf* GetSymName (const SymEntry* S)
323 /* Return the name of the symbol */
324 {
325     return GetStrBuf (S->Name);
326 }
327 #else
328 #  define GetSymName(S)   GetStrBuf ((S)->Name)
329 #endif
330
331 #if defined(HAVE_INLINE)
332 INLINE unsigned char GetSymAddrSize (const SymEntry* S)
333 /* Return the address size of the symbol. Beware: This function will just
334  * return the AddrSize member, it will not look at the expression!
335  */
336 {
337     return S->AddrSize;
338 }
339 #else
340 #  define GetSymAddrSize(S)   ((S)->AddrSize)
341 #endif
342
343 long GetSymVal (SymEntry* Sym);
344 /* Return the value of a symbol assuming it's constant. FAIL will be called
345  * in case the symbol is undefined or not constant.
346  */
347
348 unsigned GetSymImportId (const SymEntry* Sym);
349 /* Return the import id for the given symbol */
350
351 unsigned GetSymExportId (const SymEntry* Sym);
352 /* Return the export id for the given symbol */
353
354 unsigned GetSymInfoFlags (const SymEntry* Sym, long* ConstVal);
355 /* Return a set of flags used when writing symbol information into a file.
356  * If the SYM_CONST bit is set, ConstVal will contain the constant value
357  * of the symbol. The result does not include the condes count.
358  * See common/symdefs.h for more information.
359  */
360
361
362
363 /* End of symentry.h */
364
365 #endif
366
367
368
369