]> git.sur5r.net Git - cc65/blob - src/ca65/symentry.h
a7b3bf3306cc5d55f442d1ad1f4ff0fe237a40ff
[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ö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
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_LABEL        0x0080          /* Used as a label */
62 #define SF_FORCED       0x0100          /* Forced import, SF_IMPORT also set */
63 #define SF_INDEXED      0x0800          /* Index is valid */
64 #define SF_MULTDEF      0x2000          /* Multiply defined symbol */
65 #define SF_DEFINED      0x4000          /* Defined */
66 #define SF_REFERENCED   0x8000          /* Referenced */
67
68 /* Arguments for SymFind... */
69 #define SYM_FIND_EXISTING       0
70 #define SYM_ALLOC_NEW           1
71
72 /* Structure of a symbol table entry */
73 typedef struct SymEntry SymEntry;
74 struct SymEntry {
75     SymEntry*               Left;       /* Lexically smaller entry */
76     SymEntry*               Right;      /* Lexically larger entry */
77     SymEntry*               List;       /* List of all entries */
78     SymEntry*               Locals;     /* Root of subtree for local symbols */
79     struct SymTable*        SymTab;     /* Table this symbol is in, 0 for locals */
80     FilePos                 Pos;        /* File position for this symbol */
81     unsigned                Flags;      /* Symbol flags */
82     unsigned                Index;      /* Index of import/export entries */
83     union {
84         struct ExprNode*    Expr;       /* Expression if CONST not set */
85         long                Val;        /* Value (if CONST set) */
86         SymEntry*           Sym;        /* Symbol (if trampoline entry) */
87     } V;
88     Collection              ExprRefs;   /* Expressions using this symbol */
89     unsigned char           ExportSize; /* Export address size */
90     unsigned char           AddrSize;   /* Address size of label */
91     unsigned char           ConDesPrio[CD_TYPE_COUNT];  /* ConDes priorities... */
92                                         /* ...actually value+1 (used as flag) */
93     unsigned                Name;       /* Name index in global string pool */
94 };
95
96 /* List of all symbol table entries */
97 extern SymEntry* SymList;
98
99 /* Pointer to last defined symbol */
100 extern SymEntry* SymLast;
101
102
103
104 /*****************************************************************************/
105 /*                                   Code                                    */
106 /*****************************************************************************/
107
108
109
110 int IsLocalName (const char* Name);
111 /* Return true if Name is the name of a local symbol */
112
113 int IsLocalNameId (unsigned Name);
114 /* Return true if Name is the name of a local symbol */
115
116 SymEntry* NewSymEntry (const char* Name);
117 /* Allocate a symbol table entry, initialize and return it */
118
119 int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E);
120 /* Search in the given tree for a name. If we find the symbol, the function
121  * will return 0 and put the entry pointer into E. If we did not find the
122  * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
123  * E will be set to the last entry, and the result of the function is <0 if
124  * the entry should be inserted on the left side, and >0 if it should get
125  * inserted on the right side.
126  */
127
128 #if defined(HAVE_INLINE)
129 INLINE void SymAddExprRef (SymEntry* Sym, struct ExprNode* Expr)
130 /* Add an expression reference to this symbol */
131 {
132     CollAppend (&Sym->ExprRefs, Expr);
133 }
134 #else
135 #define SymAddExprRef(Sym,Expr)     CollAppend (&(Sym)->ExprRefs, Expr)
136 #endif
137
138 #if defined(HAVE_INLINE)
139 INLINE void SymDelExprRef (SymEntry* Sym, struct ExprNode* Expr)
140 /* Delete an expression reference to this symbol */
141 {
142     CollDeleteItem (&Sym->ExprRefs, Expr);
143 }
144 #else
145 #define SymDelExprRef(Sym,Expr)     CollDeleteItem (&(Sym)->ExprRefs, Expr)
146 #endif
147
148 void SymDef (SymEntry* Sym, ExprNode* Expr, unsigned char AddrSize, unsigned Flags);
149 /* Mark a symbol as defined */
150
151 void SymRef (SymEntry* Sym);
152 /* Mark the given symbol as referenced */
153
154 void SymImport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
155 /* Mark the given symbol as an imported symbol */
156
157 void SymExport (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
158 /* Mark the given symbol as an exported symbol */
159
160 void SymGlobal (SymEntry* Sym, unsigned char AddrSize, unsigned Flags);
161 /* Mark the given symbol as a global symbol, that is, as a symbol that is
162  * either imported or exported.
163  */
164
165 void SymConDes (SymEntry* Sym, unsigned char AddrSize, unsigned Type, unsigned Prio);
166 /* Mark the given symbol as a module constructor/destructor. This will also
167  * mark the symbol as an export. Initializers may never be zero page symbols.
168  */
169
170 int SymIsDef (const SymEntry* Sym);
171 /* Return true if the given symbol is already defined */
172
173 int SymIsRef (const SymEntry* Sym);
174 /* Return true if the given symbol has been referenced */
175
176 int SymIsImport (const SymEntry* Sym);
177 /* Return true if the given symbol is marked as import */
178
179 int SymIsConst (SymEntry* Sym, long* Val);
180 /* Return true if the given symbol has a constant value. If Val is not NULL
181  * and the symbol has a constant value, store it's value there.
182  */
183
184 int SymHasExpr (const SymEntry* Sym);
185 /* Return true if the given symbol has an associated expression */
186
187 void SymMarkUser (SymEntry* Sym);
188 /* Set a user mark on the specified symbol */
189
190 void SymUnmarkUser (SymEntry* Sym);
191 /* Remove a user mark from the specified symbol */
192
193 int SymHasUserMark (SymEntry* Sym);
194 /* Return the state of the user mark for the specified symbol */
195
196 struct ExprNode* GetSymExpr (SymEntry* Sym);
197 /* Get the expression for a non-const symbol */
198
199 const struct ExprNode* SymResolve (const SymEntry* Sym);
200 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
201  * NULL. Do not call in other contexts!
202  */
203
204 const char* GetSymName (const SymEntry* Sym);
205 /* Return the name of the symbol */
206
207 unsigned char GetSymAddrSize (const SymEntry* Sym);
208 /* Return the address size of the symbol. Beware: This function will just
209  * return the AddrSize member, it will not look at the expression!
210  */
211
212 long GetSymVal (SymEntry* Sym);
213 /* Return the value of a symbol assuming it's constant. FAIL will be called
214  * in case the symbol is undefined or not constant.
215  */
216
217 unsigned GetSymIndex (const SymEntry* Sym);
218 /* Return the symbol index for the given symbol */
219
220 const FilePos* GetSymPos (const SymEntry* Sym);
221 /* Return the position of first occurence in the source for the given symbol */
222
223
224
225
226 /* End of symentry.h */
227
228 #endif
229
230
231