]> git.sur5r.net Git - cc65/blob - src/cc65/symentry.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / cc65 / symentry.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                symentry.c                                 */
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 #include <string.h>
37
38 /* common */
39 #include "xmalloc.h"
40
41 /* cc65 */
42 #include "anonname.h"
43 #include "declare.h"
44 #include "error.h"
45 #include "symentry.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Code                                    */
51 /*****************************************************************************/
52
53
54
55 SymEntry* NewSymEntry (const char* Name, unsigned Flags)
56 /* Create a new symbol table with the given name */
57 {
58     /* Get the length of the name */
59     unsigned Len = strlen (Name);
60
61     /* Allocate memory for the symbol entry */
62     SymEntry* E = xmalloc (sizeof (SymEntry) + Len);
63
64     /* Initialize the entry */
65     E->NextHash = 0;
66     E->PrevSym  = 0;
67     E->NextSym  = 0;
68     E->Link     = 0;
69     E->Owner    = 0;
70     E->Flags    = Flags;
71     E->Type     = 0;
72     E->Attr     = 0;
73     E->AsmName  = 0;
74     memcpy (E->Name, Name, Len+1);
75
76     /* Return the new entry */
77     return E;
78 }
79
80
81
82 void FreeSymEntry (SymEntry* E)
83 /* Free a symbol entry */
84 {
85     TypeFree (E->Type);
86     xfree (E->AsmName);
87     xfree (E);
88 }
89
90
91
92 void DumpSymEntry (FILE* F, const SymEntry* E)
93 /* Dump the given symbol table entry to the file in readable form */
94 {
95     static const struct {
96         const char*         Name;
97         unsigned            Val;
98     } Flags [] = {
99         /* Beware: Order is important! */
100         { "SC_TYPEDEF",     SC_TYPEDEF          },
101         { "SC_BITFIELD",    SC_BITFIELD         },
102         { "SC_STRUCTFIELD", SC_STRUCTFIELD      },
103         { "SC_UNION",       SC_UNION            },
104         { "SC_STRUCT",      SC_STRUCT           },
105         { "SC_AUTO",        SC_AUTO             },
106         { "SC_REGISTER",    SC_REGISTER         },
107         { "SC_STATIC",      SC_STATIC           },
108         { "SC_EXTERN",      SC_EXTERN           },
109         { "SC_ENUM",        SC_ENUM             },
110         { "SC_CONST",       SC_CONST            },
111         { "SC_LABEL",       SC_LABEL            },
112         { "SC_PARAM",       SC_PARAM            },
113         { "SC_FUNC",        SC_FUNC             },
114         { "SC_STORAGE",     SC_STORAGE          },
115         { "SC_DEF",         SC_DEF              },
116         { "SC_REF",         SC_REF              },
117         { "SC_ZEROPAGE",    SC_ZEROPAGE         },
118     };
119
120     unsigned I;
121     unsigned SymFlags;
122
123     /* Print the name */
124     fprintf (F, "%s:\n", E->Name);
125
126     /* Print the assembler name if we have one */
127     if (E->AsmName) {
128         fprintf (F, "    AsmName: %s\n", E->AsmName);
129     }                                             
130
131     /* Print the flags */
132     SymFlags = E->Flags;
133     fprintf (F, "    Flags: ");
134     for (I = 0; I < sizeof (Flags) / sizeof (Flags[0]) && SymFlags != 0; ++I) {
135         if ((SymFlags & Flags[I].Val) == Flags[I].Val) {
136             SymFlags &= ~Flags[I].Val;
137             fprintf (F, "%s ", Flags[I].Name);
138         }
139     }
140     if (SymFlags != 0) {
141         fprintf (F, "%04X", SymFlags);
142     }
143     fprintf (F, "\n");
144
145     /* Print the type */
146     fprintf (F, "    Type:  ");
147     if (E->Type) {
148         PrintType (F, E->Type);
149     } else {
150         fprintf (F, "(none)");
151     }
152     fprintf (F, "\n");
153 }
154
155
156
157 int SymIsOutputFunc (const SymEntry* Sym)
158 /* Return true if this is a function that must be output */
159 {
160     /* Symbol must be a function which is defined and either extern or
161      * static and referenced.
162      */
163     return IsTypeFunc (Sym->Type)               &&
164            SymIsDef (Sym)                       &&
165            (Sym->Flags & (SC_REF | SC_EXTERN));
166 }
167
168
169
170 const DeclAttr* SymGetAttr (const SymEntry* Sym, DeclAttrType AttrType)
171 /* Return an attribute for this symbol or NULL if the attribute does not exist */
172 {
173     /* Beware: We may not even have a collection */
174     if (Sym->Attr) {
175         unsigned I;
176         for (I = 0; I < CollCount (Sym->Attr); ++I) {
177
178             /* Get the next attribute */
179             const DeclAttr* A = CollConstAt (Sym->Attr, I);
180
181             /* If this is the one we're searching for, return it */
182             if (A->AttrType == AttrType) {
183                 return A;
184             }
185         }
186     }
187
188     /* Not found */
189     return 0;
190 }
191
192
193
194 void SymUseAttr (SymEntry* Sym, struct Declaration* D)
195 /* Use the attributes from the declaration for this symbol */
196 {
197     /* We cannot specify attributes twice */
198     if ((Sym->Flags & SC_HAVEATTR) != 0) {
199         if (D->Attributes != 0) {
200             Error ("Attributes must be specified in the first declaration");
201         }
202         return;
203     }
204
205     /* Move the attributes */
206     Sym->Attr = D->Attributes;
207     D->Attributes = 0;
208     Sym->Flags |= SC_HAVEATTR;
209 }
210
211
212
213 void SymSetAsmName (SymEntry* Sym)
214 /* Set the assembler name for an external symbol from the name of the symbol */
215 {
216     unsigned Len;
217
218     /* Cannot be used to change the name */
219     PRECONDITION (Sym->AsmName == 0);
220
221     /* The assembler name starts with an underline */
222     Len = strlen (Sym->Name);
223     Sym->AsmName = xmalloc (Len + 2);
224     Sym->AsmName[0] = '_';
225     memcpy (Sym->AsmName+1, Sym->Name, Len+1);
226 }
227
228
229
230 void CvtRegVarToAuto (SymEntry* Sym)
231 /* Convert a register variable to an auto variable */
232 {
233     /* Change the storage class */
234     Sym->Flags = (Sym->Flags & ~(SC_REGISTER | SC_STATIC | SC_EXTERN)) | SC_AUTO;
235
236     /* Transfer the stack offset from register save area to actual offset */
237     Sym->V.Offs = Sym->V.R.SaveOffs;
238 }
239
240
241
242 void ChangeSymType (SymEntry* Entry, Type* T)
243 /* Change the type of the given symbol */
244 {
245     TypeFree (Entry->Type);
246     Entry->Type = TypeDup (T);
247 }
248
249
250
251 void ChangeAsmName (SymEntry* Entry, const char* NewAsmName)
252 /* Change the assembler name of the symbol */
253 {
254     xfree (Entry->AsmName);
255     Entry->AsmName = xstrdup (NewAsmName);
256 }
257
258
259
260 int HasAnonName (const SymEntry* Entry)
261 /* Return true if the symbol entry has an anonymous name */
262 {
263     return IsAnonName (Entry->Name);
264 }
265
266
267