]> git.sur5r.net Git - cc65/blob - src/ca65/symentry.h
5d5aadd0a2073274ccf0778ac1b392f2803f41c4
[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-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Bits for the Flags value in SymEntry */
55 #define SF_NONE         0x0000          /* Empty flag set */
56 #define SF_USER         0x0001          /* User bit */
57 #define SF_TRAMPOLINE   0x0002          /* Trampoline entry */
58 #define SF_EXPORT       0x0004          /* Export this symbol */
59 #define SF_IMPORT       0x0008          /* Import this symbol */
60 #define SF_GLOBAL       0x0010          /* Global symbol */
61 #define SF_ZP           0x0020          /* Declared as zeropage symbol */
62 #define SF_ABS          0x0040          /* Declared as absolute symbol */
63 #define SF_LABEL        0x0080          /* Used as a label */
64 #define SF_FORCED       0x0100          /* Forced import, SF_IMPORT also set */
65 #define SF_FINALIZED    0x0200          /* Symbol is finalized */
66 #define SF_INDEXED      0x0800          /* Index is valid */
67 #define SF_CONST        0x1000          /* The symbol has a constant value */
68 #define SF_MULTDEF      0x2000          /* Multiply defined symbol */
69 #define SF_DEFINED      0x4000          /* Defined */
70 #define SF_REFERENCED   0x8000          /* Referenced */
71
72 /* Flags used in SymDef */
73 #define SYM_DEFAULT     0x00
74 #define SYM_ZP          0x01
75 #define SYM_LABEL       0x02
76
77 /* Arguments for SymFind... */
78 #define SYM_FIND_EXISTING       0
79 #define SYM_ALLOC_NEW           1
80
81 /* Structure of a symbol table entry */
82 typedef struct SymEntry SymEntry;
83 struct 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 */
88     struct SymTable*        SymTab;     /* Table this symbol is in, 0 for locals */
89     FilePos                 Pos;        /* File position for this symbol */
90     unsigned                Flags;      /* Symbol flags */
91     unsigned                Index;      /* Index of import/export entries */
92     union {
93         struct ExprNode*    Expr;       /* Expression if CONST not set */
94         long                Val;        /* Value (if CONST set) */
95         SymEntry*           Sym;        /* Symbol (if trampoline entry) */
96     } V;
97     Collection              ExprRefs;   /* Expressions using this symbol */
98     unsigned char           ConDesPrio[CD_TYPE_COUNT];  /* ConDes priorities... */
99                                         /* ...actually value+1 (used as flag) */
100     unsigned                Name;       /* Name index in global string pool */
101 };
102
103 /* List of all symbol table entries */
104 extern SymEntry* SymList;
105
106 /* Pointer to last defined symbol */
107 extern SymEntry* SymLast;
108
109
110
111 /*****************************************************************************/
112 /*                                   Code                                    */
113 /*****************************************************************************/
114
115
116
117 int IsLocalName (const char* Name);
118 /* Return true if Name is the name of a local symbol */
119
120 int IsLocalNameId (unsigned Name);
121 /* Return true if Name is the name of a local symbol */
122
123 SymEntry* NewSymEntry (const char* Name);
124 /* Allocate a symbol table entry, initialize and return it */
125
126 #if defined(HAVE_INLINE)
127 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
128 /* Add an expression reference to this symbol */
129 {
130     CollAppend (&Sym->ExprRefs, Expr);
131 }
132 #else
133 #define SymAddExprRef(Sym,Expr)     CollAppend (&(Sym)->ExprRefs, Expr)
134 #endif
135
136 #if defined(HAVE_INLINE)
137 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
138 /* Delete an expression reference to this symbol */
139 {
140     CollDeleteItem (&Sym->ExprRefs, Expr);
141 }
142 #else
143 #define SymDelExprRef(Sym,Expr)     CollDeleteItem (&(Sym)->ExprRefs, Expr)
144 #endif
145
146 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned Flags);
147 /* Mark a symbol as defined */
148
149 void SymRef (SymEntry* Sym);
150 /* Mark the given symbol as referenced */
151
152 int SymIsDef (const SymEntry* Sym);
153 /* Return true if the given symbol is already defined */
154
155 int SymIsRef (const SymEntry* Sym);
156 /* Return true if the given symbol has been referenced */
157
158 int SymIsImport (const SymEntry* Sym);
159 /* Return true if the given symbol is marked as import */
160
161 int SymHasExpr (const SymEntry* Sym);
162 /* Return true if the given symbol has an associated expression */
163
164 void SymFinalize (SymEntry* S);
165 /* Finalize a symbol expression if there is one */
166
167 void SymMarkUser (SymEntry* Sym);
168 /* Set a user mark on the specified symbol */
169
170 void SymUnmarkUser (SymEntry* Sym);
171 /* Remove a user mark from the specified symbol */
172
173 int SymHasUserMark (SymEntry* Sym);
174 /* Return the state of the user mark for the specified symbol */
175
176 long GetSymVal (SymEntry* Sym);
177 /* Return the symbol value */
178
179 struct ExprNode* GetSymExpr (SymEntry* Sym);
180 /* Get the expression for a non-const symbol */
181
182 const char* GetSymName (SymEntry* Sym);
183 /* Return the name of the symbol */
184
185 unsigned GetSymIndex (SymEntry* Sym);
186 /* Return the symbol index for the given symbol */
187
188 const FilePos* GetSymPos (SymEntry* Sym);
189 /* Return the position of first occurence in the source for the given symbol */
190
191
192
193
194 /* End of symentry.h */
195
196 #endif
197
198
199