]> git.sur5r.net Git - cc65/blob - src/cc65/symentry.h
Stack adjustment code optimizations.
[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-2013, 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 */
91 #define SC_UNION        0x4002U         /* Union */
92 #define SC_STRUCTFIELD  0x4003U         /* Struct or union field */
93 #define SC_BITFIELD     0x4004U         /* A bit-field inside a struct or union */
94 #define SC_TYPEDEF      0x4005U         /* A typedef */
95 #define SC_TYPEMASK     0x400FU         /* Mask for above types */
96
97 #define SC_ZEROPAGE     0x8000U         /* Symbol marked as zeropage */
98
99 #define SC_HAVEATTR     0x10000U        /* Symbol has attributes */
100
101 #define SC_GOTO         0x20000U
102 #define SC_SPADJUSTMENT 0x40000U
103
104
105
106
107 /* Label definition or reference */
108 typedef struct DefOrRef DefOrRef;
109 struct DefOrRef {
110     unsigned            Line;
111     long                LocalsBlockId;
112     unsigned            Flags;
113     int                 StackPtr;
114     unsigned            Depth;
115     unsigned            LateSP_Label;
116 };
117
118 /* Symbol table entry */
119 typedef struct SymEntry SymEntry;
120 struct SymEntry {
121     SymEntry*                   NextHash; /* Next entry in hash list */
122     SymEntry*                   PrevSym;  /* Previous symbol in dl list */
123     SymEntry*                   NextSym;  /* Next symbol double linked list */
124     SymEntry*                   Link;     /* General purpose single linked list */
125     struct SymTable*            Owner;    /* Symbol table the symbol is in */
126     unsigned                    Flags;    /* Symbol flags */
127     Type*                       Type;     /* Symbol type */
128     Collection*                 Attr;     /* Attribute list if any */
129     char*                       AsmName;  /* Assembler name if any */
130
131     /* Data that differs for the different symbol types */
132     union {
133
134         /* Offset for locals or struct members */
135         int                     Offs;
136
137         /* Label name for static symbols */
138         struct {
139             unsigned            Label;
140             Collection          *DefsOrRefs;
141         } L;
142
143         struct {
144             unsigned short      SPAdjustment;
145         } G;
146
147         /* Register bank offset and offset of the saved copy on stack for
148         ** register variables.
149         */
150         struct {
151             int                 RegOffs;
152             int                 SaveOffs;
153         } R;
154
155         /* Value for constants (including enums) */
156         long                    ConstVal;
157
158         /* Data for structs/unions */
159         struct {
160             struct SymTable*    SymTab;   /* Member symbol table */
161             unsigned            Size;     /* Size of the union/struct */
162         } S;
163
164         /* Data for bit fields */
165         struct {
166             unsigned            Offs;     /* Byte offset into struct */
167             unsigned            BitOffs;  /* Bit offset into storage unit */
168             unsigned            BitWidth; /* Width in bits */
169         } B;
170
171         /* Data for functions */
172         struct {
173             struct FuncDesc*    Func;     /* Function descriptor */
174             struct Segments*    Seg;      /* Segments for this function */
175             struct LiteralPool* LitPool;  /* Literal pool for this function */
176         } F;
177
178         /* Segment name for tentantive global definitions */
179         const char*             BssName;
180     } V;
181     char                       Name[1]; /* Name, dynamically allocated */
182 };
183
184
185
186 /*****************************************************************************/
187 /*                                   Code                                    */
188 /*****************************************************************************/
189
190
191
192 SymEntry* NewSymEntry (const char* Name, unsigned Flags);
193 /* Create a new symbol table with the given name */
194
195 void FreeSymEntry (SymEntry* E);
196 /* Free a symbol entry */
197
198 void DumpSymEntry (FILE* F, const SymEntry* E);
199 /* Dump the given symbol table entry to the file in readable form */
200
201 #if defined(HAVE_INLINE)
202 INLINE int SymIsBitField (const SymEntry* Sym)
203 /* Return true if the given entry is a bit-field entry */
204 {
205     return ((Sym->Flags & SC_BITFIELD) == SC_BITFIELD);
206 }
207 #else
208 #  define SymIsBitField(Sym)    (((Sym)->Flags & SC_BITFIELD) == SC_BITFIELD)
209 #endif
210
211 #if defined(HAVE_INLINE)
212 INLINE int SymIsTypeDef (const SymEntry* Sym)
213 /* Return true if the given entry is a typedef entry */
214 {
215     return ((Sym->Flags & SC_TYPEDEF) == SC_TYPEDEF);
216 }
217 #else
218 #  define SymIsTypeDef(Sym)     (((Sym)->Flags & SC_TYPEDEF) == SC_TYPEDEF)
219 #endif
220
221 #if defined(HAVE_INLINE)
222 INLINE int SymIsDef (const SymEntry* Sym)
223 /* Return true if the given entry is defined */
224 {
225     return ((Sym->Flags & SC_DEF) == SC_DEF);
226 }
227 #else
228 #  define SymIsDef(Sym)     (((Sym)->Flags & SC_DEF) == SC_DEF)
229 #endif
230
231 #if defined(HAVE_INLINE)
232 INLINE int SymIsRef (const SymEntry* Sym)
233 /* Return true if the given entry is referenced */
234 {
235     return ((Sym->Flags & SC_REF) == SC_REF);
236 }
237 #else
238 #  define SymIsRef(Sym)     (((Sym)->Flags & SC_REF) == SC_REF)
239 #endif
240
241 #if defined(HAVE_INLINE)
242 INLINE int SymIsRegVar (const SymEntry* Sym)
243 /* Return true if the given entry is a register variable */
244 /* ### HACK! Fix the ugly type flags! */
245 {
246     return ((Sym->Flags & (SC_REGISTER|SC_TYPE)) == SC_REGISTER);
247 }
248 #else
249 #  define SymIsRegVar(Sym)      (((Sym)->Flags & (SC_REGISTER|SC_TYPE)) == SC_REGISTER)
250 #endif
251
252 int SymIsOutputFunc (const SymEntry* Sym);
253 /* Return true if this is a function that must be output */
254
255 #if defined(HAVE_INLINE)
256 INLINE const char* SymGetAsmName (const SymEntry* Sym)
257 /* Return the assembler label name for the symbol (beware: may be NULL!) */
258 {
259     return Sym->AsmName;
260 }
261 #else
262 #  define SymGetAsmName(Sym)      ((Sym)->AsmName)
263 #endif
264
265 const DeclAttr* SymGetAttr (const SymEntry* Sym, DeclAttrType AttrType);
266 /* Return an attribute for this symbol or NULL if the attribute does not exist */
267
268 #if defined(HAVE_INLINE)
269 INLINE int SymHasAttr (const SymEntry* Sym, DeclAttrType A)
270 /* Return true if the symbol has the given attribute */
271 {
272     return (SymGetAttr (Sym, A) != 0);
273 }
274 #else
275 #  define SymHasAttr(Sym, A)       (SymGetAttr (Sym, A) != 0)
276 #endif
277
278 void SymUseAttr (SymEntry* Sym, struct Declaration* D);
279 /* Use the attributes from the declaration for this symbol */
280
281 void SymSetAsmName (SymEntry* Sym);
282 /* Set the assembler name for an external symbol from the name of the symbol */
283
284 void CvtRegVarToAuto (SymEntry* Sym);
285 /* Convert a register variable to an auto variable */
286
287 void ChangeSymType (SymEntry* Entry, Type* T);
288 /* Change the type of the given symbol */
289
290 void ChangeAsmName (SymEntry* Entry, const char* NewAsmName);
291 /* Change the assembler name of the symbol */
292
293 int HasAnonName (const SymEntry* Entry);
294 /* Return true if the symbol entry has an anonymous name */
295
296
297
298 /* End of symentry.h */
299
300 #endif