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