1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* symtab.h */ |
4 | /* */ |
5 | /* Symbol table management 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 SYMTAB_H |
37 | #define SYMTAB_H |
38 | |
39 | |
40 | |
41 | #include <stdio.h> |
42 | |
43 | #include "datatype.h" |
44 | #include "symentry.h" |
45 | |
46 | |
47 | |
48 | /*****************************************************************************/ |
49 | /* Data */ |
50 | /*****************************************************************************/ |
51 | |
52 | |
53 | |
54 | /* Symbol table */ |
55 | typedef struct SymTable SymTable; |
56 | struct SymTable { |
57 | SymTable* PrevTab; /* Pointer to higher level symbol table */ |
58 | SymEntry* SymHead; /* Double linked list of symbols */ |
59 | SymEntry* SymTail; /* Double linked list of symbols */ |
60 | unsigned SymCount; /* Count of symbols in this table */ |
61 | unsigned Size; /* Size of table */ |
62 | SymEntry* Tab[1]; /* Actual table, dynamically allocated */ |
63 | }; |
64 | |
65 | /* An empty symbol table */ |
66 | extern SymTable EmptySymTab; |
67 | |
68 | /* Forwards */ |
69 | struct FuncDesc; |
70 | |
71 | /* Predefined lexical levels */ |
72 | #define LEX_LEVEL_GLOBAL 1U |
73 | #define LEX_LEVEL_FUNCTION 2U |
74 | |
75 | |
76 | |
77 | /*****************************************************************************/ |
78 | /* Handling of lexical levels */ |
79 | /*****************************************************************************/ |
80 | |
81 | |
82 | |
83 | unsigned GetLexicalLevel (void); |
84 | /* Return the current lexical level */ |
85 | |
86 | void EnterGlobalLevel (void); |
87 | /* Enter the program global lexical level */ |
88 | |
89 | void LeaveGlobalLevel (void); |
90 | /* Leave the program global lexical level */ |
91 | |
92 | void EnterFunctionLevel (void); |
93 | /* Enter function lexical level */ |
94 | |
95 | void RememberFunctionLevel (struct FuncDesc* F); |
96 | /* Remember the symbol tables for the level and leave the level without checks */ |
97 | |
98 | void ReenterFunctionLevel (struct FuncDesc* F); |
99 | /* Reenter the function lexical level using the existing tables from F */ |
100 | |
101 | void LeaveFunctionLevel (void); |
102 | /* Leave function lexical level */ |
103 | |
104 | void EnterBlockLevel (void); |
105 | /* Enter a nested block in a function */ |
106 | |
107 | void LeaveBlockLevel (void); |
108 | /* Leave a nested block in a function */ |
109 | |
110 | void EnterStructLevel (void); |
111 | /* Enter a nested block for a struct definition */ |
112 | |
113 | void LeaveStructLevel (void); |
114 | /* Leave a nested block for a struct definition */ |
115 | |
116 | |
117 | |
118 | /*****************************************************************************/ |
119 | /* Find functions */ |
120 | /*****************************************************************************/ |
121 | |
122 | |
123 | |
124 | SymEntry* FindSym (const char* Name); |
125 | /* Find the symbol with the given name */ |
126 | |
127 | SymEntry* FindGlobalSym (const char* Name); |
128 | /* Find the symbol with the given name in the global symbol table only */ |
129 | |
130 | SymEntry* FindLocalSym (const char* Name); |
131 | /* Find the symbol with the given name in the current symbol table only */ |
132 | |
133 | SymEntry* FindTagSym (const char* Name); |
134 | /* Find the symbol with the given name in the tag table */ |
135 | |
136 | SymEntry* FindStructField (const Type* TypeArray, const char* Name); |
137 | /* Find a struct field in the fields list */ |
138 | |
139 | unsigned short FindSPAdjustment (const char* Name); |
140 | /* Search for an entry in the table of SP adjustments */ |
141 | |
142 | |
143 | /*****************************************************************************/ |
144 | /* Add stuff to the symbol table */ |
145 | /*****************************************************************************/ |
146 | |
147 | |
148 | |
149 | SymEntry* AddStructSym (const char* Name, unsigned Type, unsigned Size, SymTable* Tab); |
150 | /* Add a struct/union entry and return it */ |
151 | |
152 | SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width); |
153 | /* Add a bit field to the local symbol table and return the symbol entry */ |
154 | |
155 | SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val); |
156 | /* Add an constant symbol to the symbol table and return it */ |
157 | |
158 | SymEntry* AddLabelSym (const char* Name, unsigned Flags); |
159 | /* Add a goto label to the symbol table */ |
160 | |
161 | SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs); |
162 | /* Add a local symbol and return the symbol entry */ |
163 | |
164 | SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags); |
165 | /* Add an external or global symbol to the symbol table and return the entry */ |
166 | |
167 | |
168 | |
169 | /*****************************************************************************/ |
170 | /* Code */ |
171 | /*****************************************************************************/ |
172 | |
173 | |
174 | |
175 | SymTable* GetSymTab (void); |
176 | /* Return the current symbol table */ |
177 | |
178 | SymTable* GetGlobalSymTab (void); |
179 | /* Return the global symbol table */ |
180 | |
181 | SymTable* GetLabelSymTab (void); |
182 | /* Return the label symbol table */ |
183 | |
184 | int SymIsLocal (SymEntry* Sym); |
185 | /* Return true if the symbol is defined in the highest lexical level */ |
186 | |
187 | void MakeZPSym (const char* Name); |
188 | /* Mark the given symbol as zero page symbol */ |
189 | |
190 | void PrintSymTable (const SymTable* Tab, FILE* F, const char* , ...); |
191 | /* Write the symbol table to the given file */ |
192 | |
193 | void EmitExternals (void); |
194 | /* Write import/export statements for external symbols */ |
195 | |
196 | void EmitDebugInfo (void); |
197 | /* Emit debug infos for the locals of the current scope */ |
198 | |
199 | |
200 | |
201 | /* End of symtab.h */ |
202 | |
203 | #endif |
204 | |