Branch data Line data Source code
1 : : /* do not edit automatically generated by mc from SymbolConversion. */
2 : : /* SymbolConversion.mod mapping between m2 symbols and gcc symbols.
3 : :
4 : : Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 : : Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 : :
7 : : This file is part of GNU Modula-2.
8 : :
9 : : GNU Modula-2 is free software; you can redistribute it and/or modify
10 : : it under the terms of the GNU General Public License as published by
11 : : the Free Software Foundation; either version 3, or (at your option)
12 : : any later version.
13 : :
14 : : GNU Modula-2 is distributed in the hope that it will be useful, but
15 : : WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU General Public License
20 : : along with GNU Modula-2; see the file COPYING3. If not see
21 : : <http://www.gnu.org/licenses/>. */
22 : :
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "gcc-consolidation.h"
26 : :
27 : : #include <stdbool.h>
28 : : # if !defined (PROC_D)
29 : : # define PROC_D
30 : : typedef void (*PROC_t) (void);
31 : : typedef struct { PROC_t proc; } PROC;
32 : : # endif
33 : :
34 : : # if !defined (TRUE)
35 : : # define TRUE (1==1)
36 : : # endif
37 : :
38 : : #if defined(__cplusplus)
39 : : # undef NULL
40 : : # define NULL 0
41 : : #endif
42 : : #define _SymbolConversion_C
43 : :
44 : : #include "GSymbolConversion.h"
45 : : # include "GNameKey.h"
46 : : # include "GIndexing.h"
47 : : # include "GSymbolTable.h"
48 : : # include "GM2Error.h"
49 : : # include "GM2ALU.h"
50 : : # include "Gm2block.h"
51 : : # include "Ggcctypes.h"
52 : : # include "GM2Printf.h"
53 : : # include "GStorage.h"
54 : : # include "GSYSTEM.h"
55 : :
56 : : # define USEPOISON true
57 : : # define GGCPOISON 0x0A5A5A5A5
58 : : typedef unsigned int *SymbolConversion_PtrToCardinal;
59 : :
60 : : static Indexing_Index mod2gcc;
61 : : static void * PoisonedSymbol;
62 : :
63 : : /*
64 : : Mod2Gcc - given a modula-2 symbol, sym, return the gcc equivalent.
65 : : */
66 : :
67 : : extern "C" tree SymbolConversion_Mod2Gcc (unsigned int sym);
68 : :
69 : : /*
70 : : Gcc2Mod - given a gcc tree return the modula-2 symbol.
71 : : */
72 : :
73 : : extern "C" unsigned int SymbolConversion_Gcc2Mod (tree tree);
74 : :
75 : : /*
76 : : AddModGcc - adds the tuple [ sym, gcc ] into the database.
77 : : */
78 : :
79 : : extern "C" void SymbolConversion_AddModGcc (unsigned int sym, tree gcc);
80 : :
81 : : /*
82 : : RemoveMod2Gcc - removes the gcc symbol from the lookup table.
83 : : */
84 : :
85 : : extern "C" void SymbolConversion_RemoveMod2Gcc (unsigned int sym);
86 : :
87 : : /*
88 : : GccKnowsAbout - returns TRUE if gcc knows about the symbol, sym.
89 : : */
90 : :
91 : : extern "C" bool SymbolConversion_GccKnowsAbout (unsigned int sym);
92 : :
93 : : /*
94 : : AddTemporaryKnown - adds a temporary gcc symbol against the modula-2 sym.
95 : : */
96 : :
97 : : extern "C" void SymbolConversion_AddTemporaryKnown (unsigned int sym);
98 : :
99 : : /*
100 : : RemoveTemporaryKnown - removes the temporary symbol.
101 : : */
102 : :
103 : : extern "C" void SymbolConversion_RemoveTemporaryKnown (unsigned int sym);
104 : :
105 : : /*
106 : : Poison - poisons a symbol.
107 : : */
108 : :
109 : : extern "C" void SymbolConversion_Poison (unsigned int sym);
110 : :
111 : : /*
112 : : Mod2GccWithoutGCCPoison - given a modula-2 symbol, sym, return
113 : : the gcc equivalent, it does not check to see
114 : : whether the gcc symbol has been poisoned.
115 : : */
116 : :
117 : : static tree Mod2GccWithoutGCCPoison (unsigned int sym);
118 : :
119 : : /*
120 : : Init - create both binary trees.
121 : : */
122 : :
123 : : static void Init (void);
124 : :
125 : :
126 : : /*
127 : : Mod2GccWithoutGCCPoison - given a modula-2 symbol, sym, return
128 : : the gcc equivalent, it does not check to see
129 : : whether the gcc symbol has been poisoned.
130 : : */
131 : :
132 : 647312 : static tree Mod2GccWithoutGCCPoison (unsigned int sym)
133 : : {
134 : 647312 : NameKey_Name n;
135 : 647312 : tree tr;
136 : :
137 : 647312 : if (Indexing_InBounds (mod2gcc, sym))
138 : : {
139 : 647312 : tr = (tree) (Indexing_GetIndice (mod2gcc, sym));
140 : 647312 : if (tr == PoisonedSymbol)
141 : : {
142 : 0 : n = SymbolTable_GetSymName (sym);
143 : : /* not poisoned by the garbage collector, but by the gm2 front end. */
144 : 0 : M2Printf_printf1 ((const char *) "the gm2 front end poisoned this symbol (%a)\\n", 45, (const unsigned char *) &n, (sizeof (n)-1));
145 : 0 : M2Error_InternalError ((const char *) "attempting to use a gcc symbol which is no longer in scope", 58);
146 : : }
147 : : return tr;
148 : : }
149 : : else
150 : : {
151 : : return NULL;
152 : : }
153 : : /* static analysis guarentees a RETURN statement will be used before here. */
154 : : __builtin_unreachable ();
155 : : }
156 : :
157 : :
158 : : /*
159 : : Init - create both binary trees.
160 : : */
161 : :
162 : 15260 : static void Init (void)
163 : : {
164 : 15260 : mod2gcc = Indexing_InitIndexTuned (1, (1024*1024) / 16, 16);
165 : 15260 : Storage_ALLOCATE (&PoisonedSymbol, 1);
166 : 15260 : }
167 : :
168 : :
169 : : /*
170 : : Mod2Gcc - given a modula-2 symbol, sym, return the gcc equivalent.
171 : : */
172 : :
173 : 32157170 : extern "C" tree SymbolConversion_Mod2Gcc (unsigned int sym)
174 : : {
175 : 32157170 : NameKey_Name n;
176 : 32157170 : SymbolConversion_PtrToCardinal t;
177 : 32157170 : tree tr;
178 : :
179 : 32157170 : if (USEPOISON)
180 : : {
181 : 32157170 : if (Indexing_InBounds (mod2gcc, sym))
182 : : {
183 : 31996320 : t = (SymbolConversion_PtrToCardinal) (Indexing_GetIndice (mod2gcc, sym));
184 : 31996320 : if ((t != NULL) && ((*t) == GGCPOISON))
185 : : {
186 : 0 : M2Error_InternalError ((const char *) "gcc symbol has been poisoned", 28);
187 : : }
188 : : }
189 : : }
190 : 32157170 : if (Indexing_InBounds (mod2gcc, sym))
191 : : {
192 : 31996320 : tr = (tree) (Indexing_GetIndice (mod2gcc, sym));
193 : 31996320 : if (tr == PoisonedSymbol)
194 : : {
195 : 0 : n = SymbolTable_GetSymName (sym);
196 : : /* not poisoned by the garbage collector, but by the gm2 front end */
197 : 0 : M2Printf_printf1 ((const char *) "the gm2 front end poisoned this symbol (%a)\\n", 45, (const unsigned char *) &n, (sizeof (n)-1));
198 : 0 : M2Error_InternalError ((const char *) "attempting to use a gcc symbol which is no longer in scope", 58);
199 : : }
200 : : return tr;
201 : : }
202 : : else
203 : : {
204 : : return NULL;
205 : : }
206 : : /* static analysis guarentees a RETURN statement will be used before here. */
207 : : __builtin_unreachable ();
208 : : }
209 : :
210 : :
211 : : /*
212 : : Gcc2Mod - given a gcc tree return the modula-2 symbol.
213 : : */
214 : :
215 : 0 : extern "C" unsigned int SymbolConversion_Gcc2Mod (tree tree)
216 : : {
217 : 0 : unsigned int high;
218 : 0 : unsigned int i;
219 : :
220 : 0 : i = 1;
221 : 0 : high = Indexing_HighIndice (mod2gcc);
222 : 0 : while (i <= high)
223 : : {
224 : 0 : if ((Indexing_GetIndice (mod2gcc, i)) == tree)
225 : : {
226 : : return i;
227 : : }
228 : 0 : i += 1;
229 : : }
230 : : return SymbolTable_NulSym;
231 : : /* static analysis guarentees a RETURN statement will be used before here. */
232 : : __builtin_unreachable ();
233 : : }
234 : :
235 : :
236 : : /*
237 : : AddModGcc - adds the tuple [ sym, gcc ] into the database.
238 : : */
239 : :
240 : 11220375 : extern "C" void SymbolConversion_AddModGcc (unsigned int sym, tree gcc)
241 : : {
242 : 11220375 : tree old;
243 : 11220375 : SymbolConversion_PtrToCardinal t;
244 : :
245 : 11220375 : if (gcc == (m2block_GetErrorNode ()))
246 : : {
247 : 0 : M2Error_InternalError ((const char *) "error node generated during symbol conversion", 45);
248 : : }
249 : 11220375 : if (USEPOISON)
250 : : {
251 : 11220375 : t = (SymbolConversion_PtrToCardinal) (gcc);
252 : 11220375 : if ((gcc != ((tree) (NULL))) && ((*t) == GGCPOISON))
253 : : {
254 : 0 : M2Error_InternalError ((const char *) "gcc symbol has been poisoned", 28);
255 : : }
256 : : }
257 : 11220375 : old = SymbolConversion_Mod2Gcc (sym);
258 : 11220375 : if (old == ((tree) (NULL)))
259 : : {
260 : : /* absent - add it */
261 : 11001130 : Indexing_PutIndice (mod2gcc, sym, gcc);
262 : 11001130 : if ((Indexing_GetIndice (mod2gcc, sym)) != gcc)
263 : : {
264 : 0 : M2Error_InternalError ((const char *) "failed to add gcc <-> mod2 symbol", 33);
265 : : }
266 : 11001130 : gcc = m2block_RememberConstant (gcc);
267 : : }
268 : : /* do nothing, as it is already stored */
269 : 219245 : else if (old == gcc)
270 : : {
271 : : /* avoid dangling else. */
272 : : }
273 : 0 : else if (old == (m2block_GetErrorNode ()))
274 : : {
275 : : /* avoid dangling else. */
276 : 0 : M2Error_InternalError ((const char *) "replacing a temporary symbol (currently unexpected)", 51);
277 : : }
278 : : else
279 : : {
280 : : /* avoid dangling else. */
281 : 0 : M2Error_InternalError ((const char *) "should not be replacing a symbol", 32);
282 : : }
283 : 11220375 : if ((SymbolTable_IsConst (sym)) && (! (SymbolTable_IsValueSolved (sym))))
284 : : {
285 : 335915 : M2ALU_PushTypeOfTree (sym, gcc);
286 : 335915 : SymbolTable_PopValue (sym);
287 : : }
288 : 11220375 : }
289 : :
290 : :
291 : : /*
292 : : RemoveMod2Gcc - removes the gcc symbol from the lookup table.
293 : : */
294 : :
295 : 0 : extern "C" void SymbolConversion_RemoveMod2Gcc (unsigned int sym)
296 : : {
297 : 0 : Indexing_PutIndice (mod2gcc, sym, NULL);
298 : 0 : }
299 : :
300 : :
301 : : /*
302 : : GccKnowsAbout - returns TRUE if gcc knows about the symbol, sym.
303 : : */
304 : :
305 : 36363642 : extern "C" bool SymbolConversion_GccKnowsAbout (unsigned int sym)
306 : : {
307 : 36363642 : return (Indexing_InBounds (mod2gcc, sym)) && ((Indexing_GetIndice (mod2gcc, sym)) != NULL);
308 : : /* static analysis guarentees a RETURN statement will be used before here. */
309 : : __builtin_unreachable ();
310 : : }
311 : :
312 : :
313 : : /*
314 : : AddTemporaryKnown - adds a temporary gcc symbol against the modula-2 sym.
315 : : */
316 : :
317 : 0 : extern "C" void SymbolConversion_AddTemporaryKnown (unsigned int sym)
318 : : {
319 : : /* we add the error node against symbol, sym. We expect it to be retacted later. */
320 : 0 : Indexing_PutIndice (mod2gcc, sym, m2block_GetErrorNode ());
321 : 0 : }
322 : :
323 : :
324 : : /*
325 : : RemoveTemporaryKnown - removes the temporary symbol.
326 : : */
327 : :
328 : 0 : extern "C" void SymbolConversion_RemoveTemporaryKnown (unsigned int sym)
329 : : {
330 : 0 : if ((SymbolConversion_Mod2Gcc (sym)) == (m2block_GetErrorNode ()))
331 : : {
332 : 0 : Indexing_PutIndice (mod2gcc, sym, NULL);
333 : : }
334 : : else
335 : : {
336 : 0 : M2Error_InternalError ((const char *) "attempting to remove a symbol which is not present in the tree", 62);
337 : : }
338 : 0 : }
339 : :
340 : :
341 : : /*
342 : : Poison - poisons a symbol.
343 : : */
344 : :
345 : 673509 : extern "C" void SymbolConversion_Poison (unsigned int sym)
346 : : {
347 : 673509 : void * a;
348 : :
349 : 673509 : if (! (SymbolTable_IsConst (sym)))
350 : : {
351 : 647312 : a = Mod2GccWithoutGCCPoison (sym);
352 : 647312 : if (a != NULL)
353 : : {
354 : 644768 : Indexing_PutIndice (mod2gcc, sym, PoisonedSymbol);
355 : : }
356 : : }
357 : 673509 : }
358 : :
359 : 15260 : extern "C" void _M2_SymbolConversion_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
360 : : {
361 : 15260 : Init ();
362 : 15260 : }
363 : :
364 : 0 : extern "C" void _M2_SymbolConversion_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
365 : : {
366 : 0 : }
|