]> git.sur5r.net Git - cc65/blob - src/cc65/symentry.c
Fixed wrong code generation for
[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-2006 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 "symentry.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 SymEntry* NewSymEntry (const char* Name, unsigned Flags)
54 /* Create a new symbol table with the given name */
55 {
56     /* Get the length of the name */
57     unsigned Len = strlen (Name);
58
59     /* Allocate memory for the symbol entry */
60     SymEntry* E = xmalloc (sizeof (SymEntry) + Len);
61
62     /* Initialize the entry */
63     E->NextHash = 0;
64     E->PrevSym  = 0;
65     E->NextSym  = 0;
66     E->Link     = 0;
67     E->Owner    = 0;
68     E->Flags    = Flags;
69     E->Type     = 0;
70     E->AsmName  = 0;
71     memcpy (E->Name, Name, Len+1);
72
73     /* Return the new entry */
74     return E;
75 }
76
77
78
79 void FreeSymEntry (SymEntry* E)
80 /* Free a symbol entry */
81 {
82     TypeFree (E->Type);
83     xfree (E->AsmName);
84     xfree (E);
85 }
86
87
88
89 void DumpSymEntry (FILE* F, const SymEntry* E)
90 /* Dump the given symbol table entry to the file in readable form */
91 {
92     static const struct {
93         const char*         Name;
94         unsigned            Val;
95     } Flags [] = {
96         /* Beware: Order is important! */
97         { "SC_TYPEDEF",     SC_TYPEDEF          },
98         { "SC_STRUCTFIELD", SC_STRUCTFIELD      },
99         { "SC_STRUCT",      SC_STRUCT           },
100         { "SC_AUTO",        SC_AUTO             },
101         { "SC_REGISTER",    SC_REGISTER         },
102         { "SC_STATIC",      SC_STATIC           },
103         { "SC_EXTERN",      SC_EXTERN           },
104         { "SC_ENUM",        SC_ENUM             },
105         { "SC_CONST",       SC_CONST            },
106         { "SC_LABEL",       SC_LABEL            },
107         { "SC_PARAM",       SC_PARAM            },
108         { "SC_FUNC",        SC_FUNC             },
109         { "SC_STORAGE",     SC_STORAGE          },
110         { "SC_DEF",         SC_DEF              },
111         { "SC_REF",         SC_REF              },
112         { "SC_ZEROPAGE",    SC_ZEROPAGE         },
113     };
114
115     unsigned I;
116     unsigned SymFlags;
117
118     /* Print the name */
119     fprintf (F, "%s:\n", E->Name);
120
121     /* Print the assembler name if we have one */
122     if (E->AsmName) {
123         fprintf (F, "    AsmName: %s\n", E->AsmName);
124     }
125
126     /* Print the flags */
127     SymFlags = E->Flags;
128     fprintf (F, "    Flags: ");
129     for (I = 0; I < sizeof (Flags) / sizeof (Flags[0]) && SymFlags != 0; ++I) {
130         if ((SymFlags & Flags[I].Val) == Flags[I].Val) {
131             SymFlags &= ~Flags[I].Val;
132             fprintf (F, "%s ", Flags[I].Name);
133         }
134     }
135     if (SymFlags != 0) {
136         fprintf (F, "%04X", SymFlags);
137     }
138     fprintf (F, "\n");
139
140     /* Print the type */
141     fprintf (F, "    Type:  ");
142     if (E->Type) {
143         PrintType (F, E->Type);
144     } else {
145         fprintf (F, "(none)");
146     }
147     fprintf (F, "\n");
148 }
149
150
151
152 void CvtRegVarToAuto (SymEntry* Sym)
153 /* Convert a register variable to an auto variable */
154 {
155     /* Change the storage class */
156     Sym->Flags = (Sym->Flags & ~(SC_REGISTER | SC_STATIC | SC_EXTERN)) | SC_AUTO;
157
158     /* Transfer the stack offset from register save area to actual offset */
159     Sym->V.Offs = Sym->V.R.SaveOffs;
160 }
161
162
163
164 void ChangeSymType (SymEntry* Entry, Type* T)
165 /* Change the type of the given symbol */
166 {
167     TypeFree (Entry->Type);
168     Entry->Type = TypeDup (T);
169 }
170
171
172
173 void ChangeAsmName (SymEntry* Entry, const char* NewAsmName)
174 /* Change the assembler name of the symbol */
175 {
176     xfree (Entry->AsmName);
177     Entry->AsmName = xstrdup (NewAsmName);
178 }
179
180
181
182 int HasAnonName (const SymEntry* Entry)
183 /* Return true if the symbol entry has an anonymous name */
184 {
185     return IsAnonName (Entry->Name);
186 }
187
188
189