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