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