Mangling rules

Forum for discussion about the documentation project.
Post Reply
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Mangling rules

Post by wallyg »

I was wondering if there was a description of all of the mangling rules that FB supports in all cases. I tried finding something, but I was not successful in finding any detailed description anywhere. If I missed it I apologize.

Thanks
Wally
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Mangling rules

Post by SARG »

Maybe it partially answers : look at Extern...End Extern

If you want detail about how mangling is done precisely (eg "_Z7MyTest3i"), tell me.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Mangling rules

Post by fxm »

wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Mangling rules

Post by wallyg »

Thank you. I was just curious how it worked and wondered how many different ways have been implemented in FB.

Thanks again
Wallu
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Mangling rules

Post by dodicat »

Since it is all C++ based (g++), if you have mingw on path, then this should mangle a c++ procedure and demangle it back.
The fb name is in a comment.
(Tested 64 bit g++)
If you don't have g++ on path then only the function getdemangledname is available.

Code: Select all



#inclib "stdc++"
declare function demangle cdecl alias "__cxa_demangle"(as zstring ptr,as zstring ptr=0,as long=0,byref as long=0) as zstring ptr

function getdemangledname(mn as string) as string
dim as long k
dim as string z=*demangle(mn,0,0,k) 
print "Demangling: ";iif(len(z)=0,"Fail","OK")

select case k
case  0:print "The demangling operation succeeded."
case -1:print "A memory allocation failiure occurred."
case -2:print "mangled_name is not a valid name under the C++ ABI mangling rules."
case -3:print "One of the arguments is invalid."
end select
return z
end function


Function savefile(filename As String,p As String) As String
    Dim As Long n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:Sleep:End
    End If
    Return filename
End Function

Function loadfile(file As String) As String
    Dim As Long  f=Freefile
    If Open (file For Binary Access Read As #f)=0 Then
        Dim As String text
        If Lof(f) > 0 Then
            text = String(Lof(f), 0)
            Get #f, , text
        End If
        Close #f
        Return text
    Else
        Print file;" not found":Sleep:End
    End If
End Function

function getmangledname(cppproc As String) as string
    color 3
    Kill "mangle.o"
    savefile("mangle.cpp",cppproc)
    Print " c++ function:  "+chr(10);
    color 15
    Print loadfile("mangle.cpp")
    color 3
    Shell "g++ -c " +Curdir+ "\mangle.cpp  "
    
    Var f= loadfile("mangle.o")
    
    Var m= Mid(f,Instr(f,"_Z")) 
    Var dist=Instr(m,Chr(0))
    m=Mid(m,1,dist)
    Print "From .o   file, mangled name:  ";
    color 15
    Print m
    Kill "mangle.cpp"
    Kill "mangle.o"
    return m
End function

print "SARG's mangled name _Z7MyTest3i "
print getdemangledname("_Z7MyTest3i")
print "-----------------------------"

dim as string proc,a

'function tester(as zstring ptr,byref as double,as longint) as integer ptr
proc="int* tester(char*,double&,signed long long){return 0;};"
a=getmangledname(proc)
 print getdemangledname(a)
print
print "-----------------------------"


'function multiply(as double,as double) as double
proc="double multiply(double,double){return 0;};"

 a=getmangledname(proc)
 print getdemangledname(a)
print
print "-----------------------------"

'sub dummy(as zstring ptr,byref as integer,byref as integer)
proc="void dummy(char*, int&, int&){};" 

 a=getmangledname(proc)
 print getdemangledname(a)
print
print "-----------------------------"


'function tally(n as ZSTRING PTR,u as udt) as integer
proc="class udt{"+chr(13,10)
proc+="public:"+chr(13,10)
proc+="int x;"+chr(13,10)
proc+="};"+chr(13,10)
proc+="int tally(char * n,udt u){return 0;};"+chr(13,10)

 a=getmangledname(proc)
 print getdemangledname(a)
print
print "-----------------------------"
'sub dothis(x as double, y as string)
 proc="#include <string>"+chr(13,10)
 proc+="void dothis(double x,std::string y){};"
 a=getmangledname(proc)
print getdemangledname(a)
print
print "----------- done -----------------"

sleep

 
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Mangling rules

Post by SARG »

For fbdebugger I had to code my own demangling function, an interesting piece of code :D
Post Reply