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