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