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