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-2026 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 5290421 : static NameKey_Name StringToKey (DynamicStrings_String s)
104 : {
105 5290421 : NameKey_Name k;
106 :
107 5290421 : k = NameKey_makekey (DynamicStrings_string (s));
108 5290421 : s = DynamicStrings_KillString (s);
109 5290421 : 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 4736931 : static bool SymNeedsModulePrefix (unsigned int sym, unsigned int mod)
120 : {
121 4736931 : if (SymbolTable_IsDefImp (mod))
122 : {
123 3872718 : 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 3065248 : return M2Options_WholeProgram || (SymbolTable_IsExportQualified (sym));
132 : }
133 : }
134 864213 : else if (SymbolTable_IsModule (mod))
135 : {
136 : /* avoid dangling else. */
137 127829 : 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 4839875 : static DynamicStrings_String GetModulePrefix (DynamicStrings_String Name, unsigned int Sym, unsigned int ModSym)
152 : {
153 4839875 : if ((ModSym != SymbolTable_NulSym) && (ModSym != (SymbolTable_GetBaseModule ())))
154 : {
155 : /* avoid gcc warning by using compound statement even if not strictly necessary. */
156 4739505 : 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 4736931 : else if (SymNeedsModulePrefix (Sym, ModSym))
161 : {
162 : /* avoid dangling else. */
163 2511207 : 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 453120 : static DynamicStrings_String GetFullScopePrefix (DynamicStrings_String Name, unsigned int Scope, unsigned int Sym)
179 : {
180 453120 : if (Sym != SymbolTable_NulSym)
181 : {
182 : /* avoid gcc warning by using compound statement even if not strictly necessary. */
183 453120 : 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 453120 : else if ((SymbolTable_IsDefImp (Scope)) && (SymbolTable_IsExportQualified (Sym)))
188 : {
189 : /* avoid dangling else. */
190 388812 : return DynamicStrings_ConCatChar (DynamicStrings_ConCat (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (Scope))), DynamicStrings_Mark (Name)), '_');
191 : }
192 64308 : 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 5277831 : extern "C" NameKey_Name M2AsmUtil_GetFullSymName (unsigned int sym)
210 : {
211 5277831 : DynamicStrings_String libname;
212 5277831 : DynamicStrings_String fullsymname;
213 5277831 : DynamicStrings_String module;
214 5277831 : unsigned int scope;
215 :
216 5277831 : if ((SymbolTable_IsProcedure (sym)) && (SymbolTable_IsMonoName (sym)))
217 : {
218 440530 : return SymbolTable_GetSymName (sym);
219 : }
220 : else
221 : {
222 4837301 : scope = SymbolTable_GetScope (sym);
223 4837301 : module = GetModulePrefix (DynamicStrings_InitString ((const char *) "", 0), sym, scope);
224 4837301 : fullsymname = DynamicStrings_ConCat (module, DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym))));
225 4837301 : if (((SymbolTable_IsVar (sym)) || (SymbolTable_IsProcedure (sym))) && (SymbolTable_IsExportQualified (sym)))
226 : {
227 2414777 : while (! (SymbolTable_IsDefImp (scope)))
228 : {
229 0 : scope = SymbolTable_GetScope (scope);
230 : }
231 2414777 : if ((SymbolTable_GetLibName (scope)) != NameKey_NulName)
232 : {
233 2414777 : if (Debugging)
234 : {
235 : M2Printf_printf1 ((const char *) "before sym = %s , ", 19, (const unsigned char *) &fullsymname, (sizeof (fullsymname)-1));
236 : }
237 2414777 : libname = DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetLibName (scope)));
238 2414777 : if (! (DynamicStrings_EqualArray (libname, (const char *) "", 0)))
239 : {
240 2397691 : if (Debugging)
241 : {
242 : M2Printf_printf1 ((const char *) "libname = %s , ", 16, (const unsigned char *) &libname, (sizeof (libname)-1));
243 : }
244 2397691 : 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 4837301 : 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 453120 : extern "C" NameKey_Name M2AsmUtil_GetFullScopeAsmName (unsigned int sym)
266 : {
267 453120 : DynamicStrings_String leader;
268 453120 : unsigned int scope;
269 :
270 453120 : scope = SymbolTable_GetScope (sym);
271 453120 : if (m2configure_UseUnderscoreForC)
272 : {
273 : leader = DynamicStrings_InitString ((const char *) "_", 1);
274 : }
275 : else
276 : {
277 453120 : leader = DynamicStrings_InitString ((const char *) "", 0);
278 : }
279 453120 : 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 453120 : 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 14952 : extern "C" void _M2_M2AsmUtil_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
292 : {
293 14952 : }
294 :
295 0 : extern "C" void _M2_M2AsmUtil_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
296 : {
297 0 : }
|