Branch data Line data Source code
1 : : /* do not edit automatically generated by mc from M2AsmUtil. */
2 : : /* M2AsmUtil.mod provides utilities relating symbols in the SymbolTable.
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 (FALSE)
35 : : # define FALSE (1==0)
36 : : # endif
37 : :
38 : : #define _M2AsmUtil_C
39 : :
40 : : #include "GM2AsmUtil.h"
41 : : # include "GSFIO.h"
42 : : # include "GFIO.h"
43 : : # include "GDynamicStrings.h"
44 : : # include "GStdIO.h"
45 : : # include "GStrIO.h"
46 : : # include "GNameKey.h"
47 : : # include "GM2Options.h"
48 : : # include "GM2Printf.h"
49 : : # include "GSymbolTable.h"
50 : : # include "GM2Error.h"
51 : : # include "Gm2configure.h"
52 : :
53 : : # define Debugging false
54 : :
55 : : /*
56 : : GetFullSymName - returns the NameKey for the symbol name (which also
57 : : may contain the module name).
58 : : */
59 : :
60 : : extern "C" NameKey_Name M2AsmUtil_GetFullSymName (unsigned int sym);
61 : :
62 : : /*
63 : : GetFullScopeAsmName - returns the fully qualified name for the symbol.
64 : : This will take the format
65 : : [DefImpModule|Module]_{InnerModule}_{Procedure}_SymbolName
66 : : */
67 : :
68 : : extern "C" NameKey_Name M2AsmUtil_GetFullScopeAsmName (unsigned int sym);
69 : :
70 : : /*
71 : : StringToKey - returns a Name, from a string and destroys the string.
72 : : */
73 : :
74 : : static NameKey_Name StringToKey (DynamicStrings_String s);
75 : :
76 : : /*
77 : : SymNeedsModulePrefix - return TRUE if symbol mod is required to have a prefix.
78 : : */
79 : :
80 : : static bool SymNeedsModulePrefix (unsigned int sym, unsigned int mod);
81 : :
82 : : /*
83 : : GetModulePrefix - returns a String containing the module prefix
84 : : for module, ModSym, providing symbol, Sym, is exported.
85 : : Name is marked if it is appended onto the new string.
86 : : */
87 : :
88 : : static DynamicStrings_String GetModulePrefix (DynamicStrings_String Name, unsigned int Sym, unsigned int ModSym);
89 : :
90 : : /*
91 : : GetFullScopePrefix - returns a String containing the full scope prefix
92 : : for symbol, Sym. It honours IsExportQualified.
93 : : Name is marked if it is appended onto the new string.
94 : : */
95 : :
96 : : static DynamicStrings_String GetFullScopePrefix (DynamicStrings_String Name, unsigned int Scope, unsigned int Sym);
97 : :
98 : :
99 : : /*
100 : : StringToKey - returns a Name, from a string and destroys the string.
101 : : */
102 : :
103 : 3606849 : static NameKey_Name StringToKey (DynamicStrings_String s)
104 : : {
105 : 3606849 : NameKey_Name k;
106 : :
107 : 3606849 : k = NameKey_makekey (DynamicStrings_string (s));
108 : 3606849 : s = DynamicStrings_KillString (s);
109 : 3606849 : return k;
110 : : /* static analysis guarentees a RETURN statement will be used before here. */
111 : : __builtin_unreachable ();
112 : : }
113 : :
114 : :
115 : : /*
116 : : SymNeedsModulePrefix - return TRUE if symbol mod is required to have a prefix.
117 : : */
118 : :
119 : 3105357 : static bool SymNeedsModulePrefix (unsigned int sym, unsigned int mod)
120 : : {
121 : 3105357 : if (SymbolTable_IsDefImp (mod))
122 : : {
123 : 2408604 : if (SymbolTable_IsExportUnQualified (sym))
124 : : {
125 : : return false;
126 : : }
127 : : else
128 : : {
129 : : /* We need to force the prefix if whole program is used otherwise
130 : : local symbols from multipl modules might conflict. */
131 : 2012304 : return M2Options_WholeProgram || (SymbolTable_IsExportQualified (sym));
132 : : }
133 : : }
134 : 696753 : else if (SymbolTable_IsModule (mod))
135 : : {
136 : : /* avoid dangling else. */
137 : 125260 : return M2Options_WholeProgram;
138 : : }
139 : : return false;
140 : : /* static analysis guarentees a RETURN statement will be used before here. */
141 : : __builtin_unreachable ();
142 : : }
143 : :
144 : :
145 : : /*
146 : : GetModulePrefix - returns a String containing the module prefix
147 : : for module, ModSym, providing symbol, Sym, is exported.
148 : : Name is marked if it is appended onto the new string.
149 : : */
150 : :
151 : 3195213 : static DynamicStrings_String GetModulePrefix (DynamicStrings_String Name, unsigned int Sym, unsigned int ModSym)
152 : : {
153 : 3195213 : if ((ModSym != SymbolTable_NulSym) && (ModSym != (SymbolTable_GetBaseModule ())))
154 : : {
155 : : /* avoid gcc warning by using compound statement even if not strictly necessary. */
156 : 3107931 : if ((SymbolTable_IsInnerModule (Sym)) || (SymbolTable_IsInnerModule (ModSym)))
157 : : {
158 : 2574 : return DynamicStrings_ConCat (DynamicStrings_ConCatChar (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (ModSym))), '_'), GetModulePrefix (Name, ModSym, SymbolTable_GetScope (ModSym)));
159 : : }
160 : 3105357 : else if (SymNeedsModulePrefix (Sym, ModSym))
161 : : {
162 : : /* avoid dangling else. */
163 : 1617717 : return DynamicStrings_ConCatChar (DynamicStrings_ConCat (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (ModSym))), DynamicStrings_Mark (Name)), '_');
164 : : }
165 : : }
166 : : return Name;
167 : : /* static analysis guarentees a RETURN statement will be used before here. */
168 : : __builtin_unreachable ();
169 : : }
170 : :
171 : :
172 : : /*
173 : : GetFullScopePrefix - returns a String containing the full scope prefix
174 : : for symbol, Sym. It honours IsExportQualified.
175 : : Name is marked if it is appended onto the new string.
176 : : */
177 : :
178 : 414210 : static DynamicStrings_String GetFullScopePrefix (DynamicStrings_String Name, unsigned int Scope, unsigned int Sym)
179 : : {
180 : 414210 : if (Sym != SymbolTable_NulSym)
181 : : {
182 : : /* avoid gcc warning by using compound statement even if not strictly necessary. */
183 : 414210 : if (SymbolTable_IsInnerModule (Scope))
184 : : {
185 : 0 : return DynamicStrings_ConCat (DynamicStrings_ConCatChar (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (Scope))), '_'), GetFullScopePrefix (Name, SymbolTable_GetScope (Scope), Sym));
186 : : }
187 : 414210 : else if ((SymbolTable_IsDefImp (Scope)) && (SymbolTable_IsExportQualified (Sym)))
188 : : {
189 : : /* avoid dangling else. */
190 : 365642 : return DynamicStrings_ConCatChar (DynamicStrings_ConCat (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (Scope))), DynamicStrings_Mark (Name)), '_');
191 : : }
192 : 48568 : else if (SymbolTable_IsProcedure (Scope))
193 : : {
194 : : /* avoid dangling else. */
195 : 0 : return DynamicStrings_ConCatChar (DynamicStrings_ConCat (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (Scope))), DynamicStrings_Mark (Name)), '_');
196 : : }
197 : : }
198 : : return Name;
199 : : /* static analysis guarentees a RETURN statement will be used before here. */
200 : : __builtin_unreachable ();
201 : : }
202 : :
203 : :
204 : : /*
205 : : GetFullSymName - returns the NameKey for the symbol name (which also
206 : : may contain the module name).
207 : : */
208 : :
209 : 3536544 : extern "C" NameKey_Name M2AsmUtil_GetFullSymName (unsigned int sym)
210 : : {
211 : 3536544 : DynamicStrings_String libname;
212 : 3536544 : DynamicStrings_String fullsymname;
213 : 3536544 : DynamicStrings_String module;
214 : 3536544 : unsigned int scope;
215 : :
216 : 3536544 : if ((SymbolTable_IsProcedure (sym)) && (SymbolTable_IsMonoName (sym)))
217 : : {
218 : 343905 : return SymbolTable_GetSymName (sym);
219 : : }
220 : : else
221 : : {
222 : 3192639 : scope = SymbolTable_GetScope (sym);
223 : 3192639 : module = GetModulePrefix (DynamicStrings_InitString ((const char *) "", 0), sym, scope);
224 : 3192639 : fullsymname = DynamicStrings_ConCat (module, DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym))));
225 : 3192639 : if (((SymbolTable_IsVar (sym)) || (SymbolTable_IsProcedure (sym))) && (SymbolTable_IsExportQualified (sym)))
226 : : {
227 : 1538785 : while (! (SymbolTable_IsDefImp (scope)))
228 : : {
229 : 0 : scope = SymbolTable_GetScope (scope);
230 : : }
231 : 1538785 : if ((SymbolTable_GetLibName (scope)) != NameKey_NulName)
232 : : {
233 : 1538785 : if (Debugging)
234 : : {
235 : : M2Printf_printf1 ((const char *) "before sym = %s , ", 19, (const unsigned char *) &fullsymname, (sizeof (fullsymname)-1));
236 : : }
237 : 1538785 : libname = DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetLibName (scope)));
238 : 1538785 : if (! (DynamicStrings_EqualArray (libname, (const char *) "", 0)))
239 : : {
240 : 1519717 : if (Debugging)
241 : : {
242 : : M2Printf_printf1 ((const char *) "libname = %s , ", 16, (const unsigned char *) &libname, (sizeof (libname)-1));
243 : : }
244 : 1519717 : fullsymname = DynamicStrings_ConCat (DynamicStrings_ConCatChar (libname, '_'), fullsymname);
245 : : }
246 : : if (Debugging)
247 : : {
248 : : M2Printf_printf1 ((const char *) "after sym = %s\\n", 16, (const unsigned char *) &fullsymname, (sizeof (fullsymname)-1));
249 : : }
250 : : }
251 : : }
252 : 3192639 : return StringToKey (fullsymname);
253 : : }
254 : : /* static analysis guarentees a RETURN statement will be used before here. */
255 : : __builtin_unreachable ();
256 : : }
257 : :
258 : :
259 : : /*
260 : : GetFullScopeAsmName - returns the fully qualified name for the symbol.
261 : : This will take the format
262 : : [DefImpModule|Module]_{InnerModule}_{Procedure}_SymbolName
263 : : */
264 : :
265 : 414210 : extern "C" NameKey_Name M2AsmUtil_GetFullScopeAsmName (unsigned int sym)
266 : : {
267 : 414210 : DynamicStrings_String leader;
268 : 414210 : unsigned int scope;
269 : :
270 : 414210 : scope = SymbolTable_GetScope (sym);
271 : 414210 : if (m2configure_UseUnderscoreForC)
272 : : {
273 : : leader = DynamicStrings_InitString ((const char *) "_", 1);
274 : : }
275 : : else
276 : : {
277 : 414210 : leader = DynamicStrings_InitString ((const char *) "", 0);
278 : : }
279 : 414210 : if ((SymbolTable_IsProcedure (sym)) && (SymbolTable_IsMonoName (sym)))
280 : : {
281 : 0 : return StringToKey (DynamicStrings_ConCat (leader, DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym)))));
282 : : }
283 : : else
284 : : {
285 : 414210 : return StringToKey (DynamicStrings_ConCat (GetFullScopePrefix (leader, scope, sym), DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym)))));
286 : : }
287 : : /* static analysis guarentees a RETURN statement will be used before here. */
288 : : __builtin_unreachable ();
289 : : }
290 : :
291 : 15260 : extern "C" void _M2_M2AsmUtil_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
292 : : {
293 : 15260 : }
294 : :
295 : 0 : extern "C" void _M2_M2AsmUtil_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
296 : : {
297 : 0 : }
|