1 /*****************************************************************************/
5 /* Symbol table entry forward for the ca65 macroassembler */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
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 */
75 #define SF_REFIMP (SF_REFERENCED|SF_IMPORT) /* A ref'd import */
77 /* Arguments for SymFind... */
78 #define SYM_FIND_EXISTING 0
79 #define SYM_ALLOC_NEW 1
81 /* Structure of a symbol table entry */
82 typedef struct SymEntry 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 */
89 struct SymTable* Tab; /* Table this symbol is in */
90 struct SymEntry* Entry;
92 Collection LineInfos; /* Line infos for this symbol */
93 FilePos* GuessedUse[1]; /* File position where symbol
94 * address size was guessed, and the
95 * smallest possible addressing was NOT
96 * used. Currently only for zero page
99 unsigned Flags; /* Symbol flags */
100 unsigned DebugSymId; /* Debug symbol id */
101 unsigned ImportId; /* Id of import if this is one */
102 unsigned ExportId; /* Id of export if this is one */
103 struct ExprNode* Expr; /* Symbol expression */
104 Collection ExprRefs; /* Expressions using this symbol */
105 unsigned char ExportSize; /* Export address size */
106 unsigned char AddrSize; /* Address size of label */
107 unsigned char ConDesPrio[CD_TYPE_COUNT]; /* ConDes priorities... */
108 /* ...actually value+1 (used as flag) */
109 unsigned Name; /* Name index in global string pool */
112 /* List of all symbol table entries */
113 extern SymEntry* SymList;
115 /* Pointer to last defined symbol */
116 extern SymEntry* SymLast;
120 /*****************************************************************************/
122 /*****************************************************************************/
126 SymEntry* NewSymEntry (const StrBuf* Name, unsigned Flags);
127 /* Allocate a symbol table entry, initialize and return it */
129 int SymSearchTree (SymEntry* T, const StrBuf* Name, SymEntry** E);
130 /* Search in the given tree for a name. If we find the symbol, the function
131 * will return 0 and put the entry pointer into E. If we did not find the
132 * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
133 * E will be set to the last entry, and the result of the function is <0 if
134 * the entry should be inserted on the left side, and >0 if it should get
135 * inserted on the right side.
138 #if defined(HAVE_INLINE)
139 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
140 /* Add an expression reference to this symbol */
142 CollAppend (&Sym->ExprRefs, Expr);
145 #define SymAddExprRef(Sym,Expr) CollAppend (&(Sym)->ExprRefs, Expr)
148 #if defined(HAVE_INLINE)
149 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
150 /* Delete an expression reference to this symbol */
152 CollDeleteItem (&Sym->ExprRefs, Expr);
155 #define SymDelExprRef(Sym,Expr) CollDeleteItem (&(Sym)->ExprRefs, Expr)
158 void SymTransferExprRefs (SymEntry* From, SymEntry* To);
159 /* Transfer all expression references from one symbol to another. */
161 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned char AddrSize, unsigned Flags);
162 /* Mark a symbol as defined */
164 void SymRef (SymEntry* Sym);
165 /* Mark the given symbol as referenced */
167 void SymImport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
168 /* Mark the given symbol as an imported symbol */
170 void SymExport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
171 /* Mark the given symbol as an exported symbol */
173 void SymGlobal (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
174 /* Mark the given symbol as a global symbol, that is, as a symbol that is
175 * either imported or exported.
178 void SymConDes (SymEntry* Sym, unsigned char AddrSize, unsigned Type, unsigned Prio);
179 /* Mark the given symbol as a module constructor/destructor. This will also
180 * mark the symbol as an export. Initializers may never be zero page symbols.
183 void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize);
184 /* Mark the address size of the given symbol as guessed. The address size
185 * passed as argument is the one NOT used, because the actual address size
186 * wasn't known. Example: Zero page addressing was not used because symbol
187 * is undefined, and absolute addressing was available.
190 void SymExportFromGlobal (SymEntry* S);
191 /* Called at the end of assembly. Converts a global symbol that is defined
195 void SymImportFromGlobal (SymEntry* S);
196 /* Called at the end of assembly. Converts a global symbol that is undefined
200 #if defined(HAVE_INLINE)
201 INLINE int SymIsDef (const SymEntry* S)
202 /* Return true if the given symbol is already defined */
204 return (S->Flags & SF_DEFINED) != 0;
207 # define SymIsDef(S) (((S)->Flags & SF_DEFINED) != 0)
210 #if defined(HAVE_INLINE)
211 INLINE int SymIsRef (const SymEntry* S)
212 /* Return true if the given symbol has been referenced */
214 return (S->Flags & SF_REFERENCED) != 0;
217 # define SymIsRef(S) (((S)->Flags & SF_REFERENCED) != 0)
220 #if defined(HAVE_INLINE)
221 INLINE int SymIsImport (const SymEntry* S)
222 /* Return true if the given symbol is marked as import */
224 /* Check the import flag */
225 return (S->Flags & SF_IMPORT) != 0;
228 # define SymIsImport(S) (((S)->Flags & SF_IMPORT) != 0)
231 #if defined(HAVE_INLINE)
232 INLINE int SymIsExport (const SymEntry* S)
233 /* Return true if the given symbol is marked as export */
235 /* Check the export flag */
236 return (S->Flags & SF_EXPORT) != 0;
239 # define SymIsExport(S) (((S)->Flags & SF_EXPORT) != 0)
242 #if defined(HAVE_INLINE)
243 INLINE int SymIsVar (const SymEntry* S)
244 /* Return true if the given symbol is marked as variable */
246 /* Check the variable flag */
247 return (S->Flags & SF_VAR) != 0;
250 # define SymIsVar(S) (((S)->Flags & SF_VAR) != 0)
253 int SymIsConst (const SymEntry* Sym, long* Val);
254 /* Return true if the given symbol has a constant value. If Val is not NULL
255 * and the symbol has a constant value, store it's value there.
258 #if defined(HAVE_INLINE)
259 INLINE int SymHasExpr (const SymEntry* S)
260 /* Return true if the given symbol has an associated expression */
262 /* Check the expression */
263 return ((S->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED);
266 # define SymHasExpr(S) (((S)->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED)
269 #if defined(HAVE_INLINE)
270 INLINE void SymMarkUser (SymEntry* S)
271 /* Set a user mark on the specified symbol */
277 # define SymMarkUser(S) ((S)->Flags |= SF_USER)
280 #if defined(HAVE_INLINE)
281 INLINE void SymUnmarkUser (SymEntry* S)
282 /* Remove a user mark from the specified symbol */
285 S->Flags &= ~SF_USER;
288 # define SymUnmarkUser(S) ((S)->Flags &= ~SF_USER)
291 #if defined(HAVE_INLINE)
292 INLINE int SymHasUserMark (SymEntry* S)
293 /* Return the state of the user mark for the specified symbol */
296 return (S->Flags & SF_USER) != 0;
299 # define SymHasUserMark(S) (((S)->Flags & SF_USER) != 0)
302 struct SymTable* GetSymParentScope (SymEntry* S);
303 /* Get the parent scope of the symbol (not the one it is defined in). Return
304 * NULL if the symbol is a cheap local, or defined on global level.
307 struct ExprNode* GetSymExpr (SymEntry* Sym);
308 /* Get the expression for a non-const symbol */
310 const struct ExprNode* SymResolve (const SymEntry* Sym);
311 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
312 * NULL. Do not call in other contexts!
315 #if defined(HAVE_INLINE)
316 INLINE const StrBuf* GetSymName (const SymEntry* S)
317 /* Return the name of the symbol */
319 return GetStrBuf (S->Name);
322 # define GetSymName(S) GetStrBuf ((S)->Name)
325 #if defined(HAVE_INLINE)
326 INLINE unsigned char GetSymAddrSize (const SymEntry* S)
327 /* Return the address size of the symbol. Beware: This function will just
328 * return the AddrSize member, it will not look at the expression!
334 # define GetSymAddrSize(S) ((S)->AddrSize)
337 long GetSymVal (SymEntry* Sym);
338 /* Return the value of a symbol assuming it's constant. FAIL will be called
339 * in case the symbol is undefined or not constant.
342 unsigned GetSymImportId (const SymEntry* Sym);
343 /* Return the import id for the given symbol */
345 unsigned GetSymExportId (const SymEntry* Sym);
346 /* Return the export id for the given symbol */
348 unsigned GetSymInfoFlags (const SymEntry* Sym, long* ConstVal);
349 /* Return a set of flags used when writing symbol information into a file.
350 * If the SYM_CONST bit is set, ConstVal will contain the constant value
351 * of the symbol. The result does not include the condes count.
352 * See common/symdefs.h for more information.
357 /* End of symentry.h */