Copy Arrays

General discussion for topics related to the FreeBASIC project or its community.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Copy Arrays

Post by dodicat »

coderJeff wrote: Jan 07, 2023 17:42 fxm, dodicat, neato. I think is good approach letting fbc handle the assignment as it would give consistent results for all arrays with normal user code (and unlike what I wrote, doesn't require some new procedure full of funky operations that have to be tested separately).

A couple of users have in the past asked for something that would deal with the type names:
- something like GetMangledTypeName( integer ptr ) that would return something like "INTEGER_PTR" that can be used as single token identifier for that type. Maybe something to try that could be added to `__fb_query_symbol__` thing (if we ever get back to it). Still kind of hack-ish but maybe still a slight improvement. idk.
I thought that all fb mangled names were c++ based.
For a sub(as integer ptr)
If you have g++ on path, my previous mangler
viz

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


var m= getmangledname("void test(int*){};")
print
print getdemangledname(m)
sleep 
gives result

Code: Select all

 
  c++ function:
void test(int*){};
From .o   file, mangled name:  _Z4testPi
Demangling: OK
The demangling operation succeeded.
test(int*)
 
Which tags P in front, Pi, (like pascal language), although pascal would be pinteger for pointer to integer.
But it looks like you are looking for a non c++ mangled name parser.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Copy Arrays

Post by coderJeff »

dodicat wrote: Jan 08, 2023 19:06 I thought that all fb mangled names were c++ based.
That is true; for any symbol name that needs to be known in object files. Though to be clear (hi marcov!) the rules follow as best we can the Itanium c++ abi which is supported by g++. There are a few variations between platforms.
dodicat wrote: Jan 08, 2023 19:06 Which tags P in front, Pi, (like pascal language), although pascal would be pinteger for pointer to integer.
But it looks like you are looking for a non c++ mangled name parser.
tbh, not sure what I am looking for. I guess something easy-ish that users might use. #macros in the preprocessor are not a great solution but generics / templates while maybe better / smarter for users, would require a huge effort to add.

Code: Select all

#include once "fbc-int/symbol.bi"
namespace ns
	type T extends object
	end type
end namespace

'' example type name
#define sometype NS.T const ptr ptr

print "type name    : " & __fb_quote__( __fb_query_symbol__( fbc.fb_query_symbol.typename, sometype ) )
print "type name id : " & __fb_quote__( __fb_query_symbol__( fbc.fb_query_symbol.typenameid, sometype ) )
print "mangled id   : " __fb_quote__( __fb_query_symbol__( fbc.fb_query_symbol.mangleid, sometype ) )
OUTPUT:

Code: Select all

type name    : NS.T CONST PTR PTR
type name id : NS_T_CONST_PTR_PTR
mangled id   : PKPN2NS1TE
The idea being that a multi-token data type is converted to some unique-ish single token identifier. But if the name doesn't need to be recoverable with demangle or human readable, then maybe __fb_uniqueid_push__ & __fb_uniqueid__ might work.
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: Copy Arrays

Post by mrminecrafttnt »

I think something like this:

Code: Select all

dim as string array_Small(1 to 3)
dim as string array_Big(1 to 10)
array_Big(1 to 3) = array_small(1 to 3)
Have must be working in the next release of FreeBasic.. :)
Post Reply