]> git.sur5r.net Git - cc65/blob - src/cc65/symentry.h
Rewrote literal handling. Literals are now saved together with other function
[cc65] / src / cc65 / symentry.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                symentry.h                                 */
4 /*                                                                           */
5 /*               Symbol table entries for the cc65 C compiler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, 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 #include <stdio.h>
42
43 /* common */
44 #include "coll.h"
45 #include "inline.h"
46
47 /* cc65 */
48 #include "datatype.h"
49 #include "declattr.h"
50
51
52
53 /*****************************************************************************/
54 /*                                  Forwards                                 */
55 /*****************************************************************************/
56
57
58
59 struct Segments;
60 struct LiteralPool;
61
62
63
64 /*****************************************************************************/
65 /*                              struct SymEntry                              */
66 /*****************************************************************************/
67
68
69
70 /* Storage classes and flags */
71 #define SC_AUTO         0x0001U         /* Auto variable */
72 #define SC_REGISTER     0x0002U         /* Register variable */
73 #define SC_STATIC       0x0004U         /* Static */
74 #define SC_EXTERN       0x0008U         /* Extern linkage */
75
76 #define SC_ENUM         0x0030U         /* An enum */
77 #define SC_CONST        0x0020U         /* A numeric constant with a type */
78 #define SC_LABEL        0x0040U         /* A goto label */
79 #define SC_PARAM        0x0080U         /* A function parameter */
80 #define SC_FUNC         0x0100U         /* A function */
81
82 #define SC_DEFTYPE      0x0200U         /* Parameter has default type (=int, old style) */
83 #define SC_STORAGE      0x0400U         /* Symbol with associated storage */
84 #define SC_DEFAULT      0x0800U         /* Flag: default storage class was used */
85
86 #define SC_DEF          0x1000U         /* Symbol is defined */
87 #define SC_REF          0x2000U         /* Symbol is referenced */
88
89 #define SC_TYPE         0x4000U         /* This is a type, struct, typedef, etc. */
90 #define SC_STRUCT       0x4001U         /* Struct or union */
91 #define SC_STRUCTFIELD  0x4002U         /* Struct or union field */
92 #define SC_BITFIELD     0x4004U         /* A bit-field inside a struct or union */
93 #define SC_TYPEDEF      0x4008U         /* A typedef */
94
95 #define SC_ZEROPAGE     0x8000U         /* Symbol marked as zeropage */
96
97 #define SC_HAVEATTR     0x10000U        /* Symbol has attributes */
98
99
100
101 /* Symbol table entry */
102 typedef struct SymEntry SymEntry;
103 struct SymEntry {
104     SymEntry*                   NextHash; /* Next entry in hash list */
105     SymEntry*                   PrevSym;  /* Previous symbol in dl list */
106     SymEntry*                   NextSym;  /* Next symbol double linked list */
107     SymEntry*                   Link;     /* General purpose single linked list */
108     struct SymTable*            Owner;    /* Symbol table the symbol is in */
109     unsigned                    Flags;    /* Symbol flags */
110     Type*                       Type;     /* Symbol type */
111     Collection*                 Attr;     /* Attribute list if any */
112     char*                       AsmName;  /* Assembler name if any */
113
114     /* Data that differs for the different symbol types */
115     union {
116
117         /* Offset for locals or struct members */
118         int                     Offs;
119
120         /* Label name for static symbols */
121         unsigned                Label;
122
123         /* Register bank offset and offset of the saved copy on stack for
124          * register variables.
125          */
126         struct {
127             int                 RegOffs;
128             int                 SaveOffs;
129         } R;
130
131         /* Value for constants (including enums) */
132         long                    ConstVal;
133
134         /* Data for structs/unions */
135         struct {
136             struct SymTable*    SymTab;   /* Member symbol table */
137             unsigned            Size;     /* Size of the union/struct */
138         } S;
139
140         /* Data for bit fields */
141         struct {
142             unsigned            Offs;     /* Byte offset into struct */
143             unsigned            BitOffs;  /* Bit offset into storage unit */
144             unsigned            BitWidth; /* Width in bits */
145         } B;
146
147         /* Data for functions */
148         struct {
149             struct FuncDesc*    Func;     /* Function descriptor */
150             struct Segments*    Seg;      /* Segments for this function */
151             struct LiteralPool* LitPool;  /* Literal pool for this function */
152         } F;
153
154     } V;
155     char                       Name[1]; /* Name, dynamically allocated */
156 };
157
158
159
160 /*****************************************************************************/
161 /*                                   Code                                    */
162 /*****************************************************************************/
163
164
165
166 SymEntry* NewSymEntry (const char* Name, unsigned Flags);
167 /* Create a new symbol table with the given name */
168
169 void FreeSymEntry (SymEntry* E);
170 /* Free a symbol entry */
171
172 void DumpSymEntry (FILE* F, const SymEntry* E);
173 /* Dump the given symbol table entry to the file in readable form */
174
175 #if defined(HAVE_INLINE)
176 INLINE int SymIsBitField (const SymEntry* Sym)
177 /* Return true if the given entry is a bit-field entry */
178 {
179     return ((Sym->Flags & SC_BITFIELD) == SC_BITFIELD);
180 }
181 #else
182 #  define SymIsBitField(Sym)    (((Sym)->Flags & SC_BITFIELD) == SC_BITFIELD)
183 #endif
184
185 #if defined(HAVE_INLINE)
186 INLINE int SymIsTypeDef (const SymEntry* Sym)
187 /* Return true if the given entry is a typedef entry */
188 {
189     return ((Sym->Flags & SC_TYPEDEF) == SC_TYPEDEF);
190 }
191 #else
192 #  define SymIsTypeDef(Sym)     (((Sym)->Flags & SC_TYPEDEF) == SC_TYPEDEF)
193 #endif
194
195 #if defined(HAVE_INLINE)
196 INLINE int SymIsDef (const SymEntry* Sym)
197 /* Return true if the given entry is defined */
198 {
199     return ((Sym->Flags & SC_DEF) == SC_DEF);
200 }
201 #else
202 #  define SymIsDef(Sym)     (((Sym)->Flags & SC_DEF) == SC_DEF)
203 #endif
204
205 #if defined(HAVE_INLINE)
206 INLINE int SymIsRef (const SymEntry* Sym)
207 /* Return true if the given entry is referenced */
208 {
209     return ((Sym->Flags & SC_REF) == SC_REF);
210 }
211 #else
212 #  define SymIsRef(Sym)     (((Sym)->Flags & SC_REF) == SC_REF)
213 #endif
214
215 #if defined(HAVE_INLINE)
216 INLINE int SymIsRegVar (const SymEntry* Sym)
217 /* Return true if the given entry is a register variable */
218 /* ### HACK! Fix the ugly type flags! */
219 {
220     return ((Sym->Flags & (SC_REGISTER|SC_TYPE)) == SC_REGISTER);
221 }
222 #else
223 #  define SymIsRegVar(Sym)      (((Sym)->Flags & (SC_REGISTER|SC_TYPE)) == SC_REGISTER)
224 #endif
225
226 int SymIsOutputFunc (const SymEntry* Sym);
227 /* Return true if this is a function that must be output */
228
229 #if defined(HAVE_INLINE)
230 INLINE const char* SymGetAsmName (const SymEntry* Sym)
231 /* Return the assembler label name for the symbol (beware: may be NULL!) */
232 {
233     return Sym->AsmName;
234 }
235 #else
236 #  define SymGetAsmName(Sym)      ((Sym)->AsmName)
237 #endif
238
239 const DeclAttr* SymGetAttr (const SymEntry* Sym, DeclAttrType AttrType);
240 /* Return an attribute for this symbol or NULL if the attribute does not exist */
241
242 #if defined(HAVE_INLINE)
243 INLINE int SymHasAttr (const SymEntry* Sym, DeclAttrType A)
244 /* Return true if the symbol has the given attribute */
245 {
246     return (SymGetAttr (Sym, A) != 0);
247 }
248 #else
249 #  define SymHasAttr(Sym, A)       (SymGetAttr (Sym, A) != 0)
250 #endif
251
252 void SymUseAttr (SymEntry* Sym, struct Declaration* D);
253 /* Use the attributes from the declaration for this symbol */
254
255 void CvtRegVarToAuto (SymEntry* Sym);
256 /* Convert a register variable to an auto variable */
257
258 void ChangeSymType (SymEntry* Entry, Type* T);
259 /* Change the type of the given symbol */
260
261 void ChangeAsmName (SymEntry* Entry, const char* NewAsmName);
262 /* Change the assembler name of the symbol */
263
264 int HasAnonName (const SymEntry* Entry);
265 /* Return true if the symbol entry has an anonymous name */
266
267
268
269 /* End of symentry.h */
270 #endif
271
272
273