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