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