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