1 /*****************************************************************************/
5 /* Symbol table entry forward for the ca65 macroassembler */
9 /* (C) 1998-2010, 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 */
74 /* Arguments for SymFind... */
75 #define SYM_FIND_EXISTING 0
76 #define SYM_ALLOC_NEW 1
78 /* Structure of a symbol table entry */
79 typedef struct SymEntry 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 */
86 struct SymTable* Tab; /* Table this symbol is in */
87 struct SymEntry* Entry;
89 FilePos Pos; /* File position 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
96 unsigned Flags; /* Symbol flags */
97 unsigned ImportId; /* Id if imported symbol */
98 struct ExprNode* Expr; /* Symbol expression */
99 Collection ExprRefs; /* Expressions using this symbol */
100 unsigned char ExportSize; /* Export address size */
101 unsigned char AddrSize; /* Address size of label */
102 unsigned char ConDesPrio[CD_TYPE_COUNT]; /* ConDes priorities... */
103 /* ...actually value+1 (used as flag) */
104 unsigned Name; /* Name index in global string pool */
107 /* List of all symbol table entries */
108 extern SymEntry* SymList;
110 /* Pointer to last defined symbol */
111 extern SymEntry* SymLast;
115 /*****************************************************************************/
117 /*****************************************************************************/
121 SymEntry* NewSymEntry (const StrBuf* Name, unsigned Flags);
122 /* Allocate a symbol table entry, initialize and return it */
124 int SymSearchTree (SymEntry* T, const StrBuf* Name, SymEntry** E);
125 /* Search in the given tree for a name. If we find the symbol, the function
126 * will return 0 and put the entry pointer into E. If we did not find the
127 * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
128 * E will be set to the last entry, and the result of the function is <0 if
129 * the entry should be inserted on the left side, and >0 if it should get
130 * inserted on the right side.
133 #if defined(HAVE_INLINE)
134 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
135 /* Add an expression reference to this symbol */
137 CollAppend (&Sym->ExprRefs, Expr);
140 #define SymAddExprRef(Sym,Expr) CollAppend (&(Sym)->ExprRefs, Expr)
143 #if defined(HAVE_INLINE)
144 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
145 /* Delete an expression reference to this symbol */
147 CollDeleteItem (&Sym->ExprRefs, Expr);
150 #define SymDelExprRef(Sym,Expr) CollDeleteItem (&(Sym)->ExprRefs, Expr)
153 void SymTransferExprRefs (SymEntry* From, SymEntry* To);
154 /* Transfer all expression references from one symbol to another. */
156 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned char AddrSize, unsigned Flags);
157 /* Mark a symbol as defined */
159 void SymRef (SymEntry* Sym);
160 /* Mark the given symbol as referenced */
162 void SymImport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
163 /* Mark the given symbol as an imported symbol */
165 void SymExport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
166 /* Mark the given symbol as an exported symbol */
168 void SymGlobal (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
169 /* Mark the given symbol as a global symbol, that is, as a symbol that is
170 * either imported or exported.
173 void SymConDes (SymEntry* Sym, unsigned char AddrSize, unsigned Type, unsigned Prio);
174 /* Mark the given symbol as a module constructor/destructor. This will also
175 * mark the symbol as an export. Initializers may never be zero page symbols.
178 void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize);
179 /* Mark the address size of the given symbol as guessed. The address size
180 * passed as argument is the one NOT used, because the actual address size
181 * wasn't known. Example: Zero page addressing was not used because symbol
182 * is undefined, and absolute addressing was available.
185 void SymExportFromGlobal (SymEntry* S);
186 /* Called at the end of assembly. Converts a global symbol that is defined
190 void SymImportFromGlobal (SymEntry* S);
191 /* Called at the end of assembly. Converts a global symbol that is undefined
195 #if defined(HAVE_INLINE)
196 INLINE int SymIsDef (const SymEntry* S)
197 /* Return true if the given symbol is already defined */
199 return (S->Flags & SF_DEFINED) != 0;
202 # define SymIsDef(S) (((S)->Flags & SF_DEFINED) != 0)
205 #if defined(HAVE_INLINE)
206 INLINE int SymIsRef (const SymEntry* S)
207 /* Return true if the given symbol has been referenced */
209 return (S->Flags & SF_REFERENCED) != 0;
212 # define SymIsRef(S) (((S)->Flags & SF_REFERENCED) != 0)
215 #if defined(HAVE_INLINE)
216 INLINE int SymIsImport (const SymEntry* S)
217 /* Return true if the given symbol is marked as import */
219 /* Check the import flag */
220 return (S->Flags & SF_IMPORT) != 0;
223 # define SymIsImport(S) (((S)->Flags & SF_IMPORT) != 0)
226 #if defined(HAVE_INLINE)
227 INLINE int SymIsExport (const SymEntry* S)
228 /* Return true if the given symbol is marked as export */
230 /* Check the export flag */
231 return (S->Flags & SF_EXPORT) != 0;
234 # define SymIsExport(S) (((S)->Flags & SF_EXPORT) != 0)
237 #if defined(HAVE_INLINE)
238 INLINE int SymIsVar (const SymEntry* S)
239 /* Return true if the given symbol is marked as variable */
241 /* Check the variable flag */
242 return (S->Flags & SF_VAR) != 0;
245 # define SymIsVar(S) (((S)->Flags & SF_VAR) != 0)
248 int SymIsConst (SymEntry* Sym, long* Val);
249 /* Return true if the given symbol has a constant value. If Val is not NULL
250 * and the symbol has a constant value, store it's value there.
253 #if defined(HAVE_INLINE)
254 INLINE int SymHasExpr (const SymEntry* S)
255 /* Return true if the given symbol has an associated expression */
257 /* Check the expression */
258 return ((S->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED);
261 # define SymHasExpr(S) (((S)->Flags & (SF_DEFINED|SF_IMPORT)) == SF_DEFINED)
264 #if defined(HAVE_INLINE)
265 INLINE void SymMarkUser (SymEntry* S)
266 /* Set a user mark on the specified symbol */
272 # define SymMarkUser(S) ((S)->Flags |= SF_USER)
275 #if defined(HAVE_INLINE)
276 INLINE void SymUnmarkUser (SymEntry* S)
277 /* Remove a user mark from the specified symbol */
280 S->Flags &= ~SF_USER;
283 # define SymUnmarkUser(S) ((S)->Flags &= ~SF_USER)
286 #if defined(HAVE_INLINE)
287 INLINE int SymHasUserMark (SymEntry* S)
288 /* Return the state of the user mark for the specified symbol */
291 return (S->Flags & SF_USER) != 0;
294 # define SymHasUserMark(S) (((S)->Flags & SF_USER) != 0)
297 struct SymTable* GetSymParentScope (SymEntry* S);
298 /* Get the parent scope of the symbol (not the one it is defined in). Return
299 * NULL if the symbol is a cheap local, or defined on global level.
302 struct ExprNode* GetSymExpr (SymEntry* Sym);
303 /* Get the expression for a non-const symbol */
305 const struct ExprNode* SymResolve (const SymEntry* Sym);
306 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
307 * NULL. Do not call in other contexts!
310 #if defined(HAVE_INLINE)
311 INLINE const StrBuf* GetSymName (const SymEntry* S)
312 /* Return the name of the symbol */
314 return GetStrBuf (S->Name);
317 # define GetSymName(S) GetStrBuf ((S)->Name)
320 #if defined(HAVE_INLINE)
321 INLINE unsigned char GetSymAddrSize (const SymEntry* S)
322 /* Return the address size of the symbol. Beware: This function will just
323 * return the AddrSize member, it will not look at the expression!
329 # define GetSymAddrSize(S) ((S)->AddrSize)
332 long GetSymVal (SymEntry* Sym);
333 /* Return the value of a symbol assuming it's constant. FAIL will be called
334 * in case the symbol is undefined or not constant.
337 unsigned GetSymImportId (const SymEntry* Sym);
338 /* Return the import id for the given symbol */
340 #if defined(HAVE_INLINE)
341 INLINE const FilePos* GetSymPos (const SymEntry* S)
342 /* Return the position of first occurence in the source for the given symbol */
347 # define GetSymPos(S) (&(S)->Pos)
352 /* End of symentry.h */