1 (* M2AsmUtil.mod provides utilities relating symbols in the SymbolTable.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
22 IMPLEMENTATION MODULE M2AsmUtil ;
25 FROM SFIO IMPORT WriteS ;
26 FROM FIO IMPORT StdOut ;
27 FROM DynamicStrings IMPORT String, string, ConCat, KillString, InitString, Mark, InitStringCharStar, ConCatChar, EqualArray ;
28 FROM StdIO IMPORT Write ;
29 FROM StrIO IMPORT WriteString ;
30 FROM NameKey IMPORT WriteKey, GetKey, MakeKey, makekey, KeyToCharStar, NulName ;
31 FROM M2Options IMPORT WholeProgram ;
32 FROM M2Printf IMPORT printf1 ;
34 FROM SymbolTable IMPORT NulSym,
35 GetSymName, GetLibName,
43 IsExportQualified, IsExportUnQualified,
44 IsExported, IsPublic, IsExtern, IsMonoName,
47 FROM M2Error IMPORT InternalError ;
48 FROM m2configure IMPORT UseUnderscoreForC ;
56 StringToKey - returns a Name, from a string and destroys the string.
59 PROCEDURE StringToKey (s: String) : Name ;
63 k := makekey (string (s)) ;
70 GetFullScopeAsmName - returns the fully qualified name for the symbol.
71 This will take the format
72 [DefImpModule|Module]_{InnerModule}_{Procedure}_SymbolName
75 PROCEDURE GetFullScopeAsmName (sym: CARDINAL) : Name ;
80 scope := GetScope (sym) ;
83 leader := InitString ('_')
85 leader := InitString ('')
87 IF IsProcedure (sym) AND IsMonoName (sym)
89 RETURN StringToKey (ConCat (leader, InitStringCharStar (KeyToCharStar (GetSymName (sym)))))
92 RETURN StringToKey (ConCat (GetFullScopePrefix (leader, scope, sym),
93 InitStringCharStar (KeyToCharStar (GetSymName (sym)))))
95 END GetFullScopeAsmName ;
99 GetFullSymName - returns the NameKey for the symbol name (which also
100 may contain the module name).
103 PROCEDURE GetFullSymName (sym: CARDINAL) : Name ;
110 IF IsProcedure (sym) AND IsMonoName (sym)
112 RETURN GetSymName (sym)
114 scope := GetScope (sym) ;
115 module := GetModulePrefix (InitString (''), sym, scope) ;
116 fullsymname := ConCat (module, InitStringCharStar (KeyToCharStar (GetSymName (sym)))) ;
117 IF (IsVar (sym) OR IsProcedure (sym)) AND IsExportQualified (sym)
119 WHILE NOT IsDefImp (scope) DO
120 scope := GetScope (scope)
122 IF GetLibName (scope) # NulName
126 printf1 ("before sym = %s , ", fullsymname)
128 libname := InitStringCharStar (KeyToCharStar (GetLibName (scope))) ;
129 IF NOT EqualArray (libname, '')
133 printf1 ("libname = %s , ", libname)
135 fullsymname := ConCat (ConCatChar (libname, '_'), fullsymname) ;
139 printf1 ("after sym = %s\n", fullsymname)
143 RETURN StringToKey (fullsymname)
149 SymNeedsModulePrefix - return TRUE if symbol mod is required to have a prefix.
152 PROCEDURE SymNeedsModulePrefix (sym, mod: CARDINAL) : BOOLEAN ;
156 IF IsExportUnQualified (sym)
160 (* We need to force the prefix if whole program is used otherwise
161 local symbols from multipl modules might conflict. *)
162 RETURN WholeProgram OR IsExportQualified (sym)
169 END SymNeedsModulePrefix ;
173 GetModulePrefix - returns a String containing the module prefix
174 for module, ModSym, providing symbol, Sym, is exported.
175 Name is marked if it is appended onto the new string.
178 PROCEDURE GetModulePrefix (Name: String; Sym, ModSym: CARDINAL) : String ;
180 IF (ModSym#NulSym) AND (ModSym#GetBaseModule())
182 IF IsInnerModule(Sym) OR IsInnerModule(ModSym)
184 RETURN( ConCat(ConCatChar(InitStringCharStar(KeyToCharStar(GetSymName(ModSym))), '_'),
185 GetModulePrefix(Name, ModSym, GetScope(ModSym))) )
186 ELSIF SymNeedsModulePrefix (Sym, ModSym)
188 RETURN( ConCatChar(ConCat(InitStringCharStar(KeyToCharStar(GetSymName(ModSym))), Mark(Name)), '_') )
192 END GetModulePrefix ;
196 GetFullScopePrefix - returns a String containing the full scope prefix
197 for symbol, Sym. It honours IsExportQualified.
198 Name is marked if it is appended onto the new string.
201 PROCEDURE GetFullScopePrefix (Name: String; Scope, Sym: CARDINAL) : String ;
205 IF IsInnerModule(Scope)
207 RETURN( ConCat(ConCatChar(InitStringCharStar(KeyToCharStar(GetSymName(Scope))), '_'),
208 GetFullScopePrefix(Name, GetScope(Scope), Sym)) )
209 ELSIF IsDefImp(Scope) AND IsExportQualified(Sym)
211 RETURN( ConCatChar(ConCat(InitStringCharStar(KeyToCharStar(GetSymName(Scope))), Mark(Name)), '_') )
212 ELSIF IsProcedure(Scope)
214 RETURN( ConCatChar(ConCat(InitStringCharStar(KeyToCharStar(GetSymName(Scope))), Mark(Name)), '_') )
218 END GetFullScopePrefix ;