1 /*****************************************************************************/
5 /* Symbol table entry forward for the ca65 macroassembler */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Bits for the Flags value in SymEntry */
59 #define SF_NONE 0x0000 /* Empty flag set */
60 #define SF_USER 0x0001 /* User bit */
61 #define SF_UNUSED 0x0002 /* Unused entry */
62 #define SF_EXPORT 0x0004 /* Export this symbol */
63 #define SF_IMPORT 0x0008 /* Import this symbol */
64 #define SF_GLOBAL 0x0010 /* Global symbol */
65 #define SF_LOCAL 0x0020 /* Cheap local symbol */
66 #define SF_LABEL 0x0080 /* Used as a label */
67 #define SF_FORCED 0x0100 /* Forced import, SF_IMPORT also set */
68 #define SF_INDEXED 0x0800 /* Index is valid */
69 #define SF_MULTDEF 0x2000 /* Multiply defined symbol */
70 #define SF_DEFINED 0x4000 /* Defined */
71 #define SF_REFERENCED 0x8000 /* Referenced */
73 /* Arguments for SymFind... */
74 #define SYM_FIND_EXISTING 0
75 #define SYM_ALLOC_NEW 1
77 /* Structure of a symbol table entry */
78 typedef struct SymEntry SymEntry;
80 SymEntry* Left; /* Lexically smaller entry */
81 SymEntry* Right; /* Lexically larger entry */
82 SymEntry* List; /* List of all entries */
83 SymEntry* Locals; /* Root of subtree for local symbols */
84 struct SymTable* SymTab; /* Table this symbol is in, 0 for locals */
85 FilePos Pos; /* File position for this symbol */
86 unsigned Flags; /* Symbol flags */
87 unsigned Index; /* Index of import/export entries */
88 struct ExprNode* Expr; /* Symbol expression */
89 Collection ExprRefs; /* Expressions using this symbol */
90 unsigned char ExportSize; /* Export address size */
91 unsigned char AddrSize; /* Address size of label */
92 unsigned char ConDesPrio[CD_TYPE_COUNT]; /* ConDes priorities... */
93 /* ...actually value+1 (used as flag) */
94 unsigned Name; /* Name index in global string pool */
97 /* List of all symbol table entries */
98 extern SymEntry* SymList;
100 /* Pointer to last defined symbol */
101 extern SymEntry* SymLast;
105 /*****************************************************************************/
107 /*****************************************************************************/
111 SymEntry* NewSymEntry (const char* Name, unsigned Flags);
112 /* Allocate a symbol table entry, initialize and return it */
114 int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E);
115 /* Search in the given tree for a name. If we find the symbol, the function
116 * will return 0 and put the entry pointer into E. If we did not find the
117 * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
118 * E will be set to the last entry, and the result of the function is <0 if
119 * the entry should be inserted on the left side, and >0 if it should get
120 * inserted on the right side.
123 #if defined(HAVE_INLINE)
124 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
125 /* Add an expression reference to this symbol */
127 CollAppend (&Sym->ExprRefs, Expr);
130 #define SymAddExprRef(Sym,Expr) CollAppend (&(Sym)->ExprRefs, Expr)
133 #if defined(HAVE_INLINE)
134 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
135 /* Delete an expression reference to this symbol */
137 CollDeleteItem (&Sym->ExprRefs, Expr);
140 #define SymDelExprRef(Sym,Expr) CollDeleteItem (&(Sym)->ExprRefs, Expr)
143 void SymTransferExprRefs (SymEntry* From, SymEntry* To);
144 /* Transfer all expression references from one symbol to another. */
146 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned char AddrSize, unsigned Flags);
147 /* Mark a symbol as defined */
149 void SymRef (SymEntry* Sym);
150 /* Mark the given symbol as referenced */
152 void SymImport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
153 /* Mark the given symbol as an imported symbol */
155 void SymExport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
156 /* Mark the given symbol as an exported symbol */
158 void SymGlobal (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
159 /* Mark the given symbol as a global symbol, that is, as a symbol that is
160 * either imported or exported.
163 void SymConDes (SymEntry* Sym, unsigned char AddrSize, unsigned Type, unsigned Prio);
164 /* Mark the given symbol as a module constructor/destructor. This will also
165 * mark the symbol as an export. Initializers may never be zero page symbols.
168 void SymExportFromGlobal (SymEntry* S);
169 /* Called at the end of assembly. Converts a global symbol that is defined
173 void SymImportFromGlobal (SymEntry* S);
174 /* Called at the end of assembly. Converts a global symbol that is undefined
178 #if defined(HAVE_INLINE)
179 INLINE int SymIsDef (const SymEntry* S)
180 /* Return true if the given symbol is already defined */
182 return (S->Flags & SF_DEFINED) != 0;
185 # define SymIsDef(S) (((S)->Flags & SF_DEFINED) != 0)
188 #if defined(HAVE_INLINE)
189 INLINE int SymIsRef (const SymEntry* S)
190 /* Return true if the given symbol has been referenced */
192 return (S->Flags & SF_REFERENCED) != 0;
195 # define SymIsRef(S) (((S)->Flags & SF_REFERENCED) != 0)
198 #if defined(HAVE_INLINE)
199 INLINE int SymIsImport (const SymEntry* S)
200 /* Return true if the given symbol is marked as import */
202 /* Check the import flag */
203 return (S->Flags & SF_IMPORT) != 0;
206 # define SymIsImport(S) (((S)->Flags & SF_IMPORT) != 0)
209 #if defined(HAVE_INLINE)
210 INLINE int SymIsExport (const SymEntry* S)
211 /* Return true if the given symbol is marked as export */
213 /* Check the export flag */
214 return (S->Flags & SF_EXPORT) != 0;
217 # define SymIsExport(S) (((S)->Flags & SF_EXPORT) != 0)
220 int SymIsConst (SymEntry* Sym, long* Val);
221 /* Return true if the given symbol has a constant value. If Val is not NULL
222 * and the symbol has a constant value, store it's value there.
225 #if defined(HAVE_INLINE)
226 INLINE int SymHasExpr (const SymEntry* S)
227 /* Return true if the given symbol has an associated expression */
229 /* Check the expression */
230 return ((S->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED);
233 # define SymHasExpr(S) (((S)->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED)
236 #if defined(HAVE_INLINE)
237 INLINE void SymMarkUser (SymEntry* S)
238 /* Set a user mark on the specified symbol */
244 # define SymMarkUser(S) ((S)->Flags |= SF_USER)
247 #if defined(HAVE_INLINE)
248 INLINE void SymUnmarkUser (SymEntry* S)
249 /* Remove a user mark from the specified symbol */
252 S->Flags &= ~SF_USER;
255 # define SymUnmarkUser(S) ((S)->Flags &= ~SF_USER)
258 #if defined(HAVE_INLINE)
259 INLINE int SymHasUserMark (SymEntry* S)
260 /* Return the state of the user mark for the specified symbol */
263 return (S->Flags & SF_USER) != 0;
266 # define SymHasUserMark(S) (((S)->Flags & SF_USER) != 0)
269 struct SymTable* GetSymParentScope (SymEntry* S);
270 /* Get the parent scope of the symbol (not the one it is defined in). Return
271 * NULL if the symbol is a cheap local, or defined on global level.
274 struct ExprNode* GetSymExpr (SymEntry* Sym);
275 /* Get the expression for a non-const symbol */
277 const struct ExprNode* SymResolve (const SymEntry* Sym);
278 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
279 * NULL. Do not call in other contexts!
282 #if defined(HAVE_INLINE)
283 INLINE const char* GetSymName (const SymEntry* S)
284 /* Return the name of the symbol */
286 return GetString (S->Name);
289 # define GetSymName(S) GetString ((S)->Name)
292 #if defined(HAVE_INLINE)
293 INLINE unsigned char GetSymAddrSize (const SymEntry* S)
294 /* Return the address size of the symbol. Beware: This function will just
295 * return the AddrSize member, it will not look at the expression!
301 # define GetSymAddrSize(S) ((S)->AddrSize)
304 long GetSymVal (SymEntry* Sym);
305 /* Return the value of a symbol assuming it's constant. FAIL will be called
306 * in case the symbol is undefined or not constant.
309 unsigned GetSymIndex (const SymEntry* Sym);
310 /* Return the symbol index for the given symbol */
312 #if defined(HAVE_INLINE)
313 INLINE const FilePos* GetSymPos (const SymEntry* S)
314 /* Return the position of first occurence in the source for the given symbol */
319 # define GetSymPos(S) (&(S)->Pos)
324 /* End of symentry.h */