Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

C backend: Add possible (but disabled) patch for STDCALL_MS/PASCAL ma…

…ngling issues
  • Loading branch information...
commit 354437e1cfd448d05094ad9a1cb51ee269592f89 1 parent cff4fa5
@dkl dkl authored
Showing with 73 additions and 42 deletions.
  1. +61 −25 compiler/ir-hlc.bas
  2. +12 −17 todo.txt
View
86 compiler/ir-hlc.bas
@@ -196,9 +196,26 @@ private function hEmitProcHeader _
byval options as EMITPROC_OPTIONS _
) as string
- dim as string ln
+ dim as string ln, mangled
+
+ if( options = 0 ) then
+ '' ctor/dtor flags on bodies
+ if( symbGetIsGlobalCtor( proc ) ) then
+ ln += "__attribute__((constructor)) "
+ elseif( symbGetIsGlobalDtor( proc ) ) then
+ ln += "__attribute__((destructor)) "
+ end if
+
+ if( proc->attrib and FB_SYMBATTRIB_NAKED ) then
+ ln += "__attribute__((naked)) "
+ end if
+ end if
if( (options and EMITPROC_ISPROCPTR) = 0 ) then
+ if( symbIsExport( proc ) ) then
+ ln += "__declspec(dllexport) "
+ end if
+
if( symbIsPrivate( proc ) ) then
ln += "static "
end if
@@ -218,20 +235,22 @@ private function hEmitProcHeader _
'' cycling through parameters of Pascal functions. Together with Stdcall
'' this results in a double-reverse resulting in the proper ABI.
''
- select case as const( symbGetProcMode( proc ) )
+ select case( symbGetProcMode( proc ) )
case FB_FUNCMODE_STDCALL, FB_FUNCMODE_STDCALL_MS, FB_FUNCMODE_PASCAL
ln += " __attribute__((stdcall))"
end select
ln += " "
+ mangled = *symbGetMangledName( proc )
+
'' Identifier
if( options and EMITPROC_ISPROCPTR ) then
ln += "(*"
- end if
- ln += *symbGetMangledName( proc )
- if( options and EMITPROC_ISPROCPTR ) then
+ ln += mangled
ln += ")"
+ else
+ ln += mangled
end if
'' Parameter list
@@ -310,6 +329,31 @@ private function hEmitProcHeader _
ln += " )"
+ if( ((options and EMITPROC_ISPROCPTR) = 0) and _
+ ((options and EMITPROC_ISPROTO) <> 0) ) then
+#if 0
+ '' Add an extra <asm("mangledname")> to prevent gcc
+ '' from adding the stdcall @N suffix. asm() can only
+ '' be used on prototypes.
+ select case( symbGetProcMode( proc ) )
+ case FB_FUNCMODE_STDCALL_MS, FB_FUNCMODE_PASCAL
+ '' Must manually add an underscore prefix if the
+ '' target requires it, because symb-mangling
+ '' won't do that for -gen gcc.
+ if( env.target.underprefix ) then
+ mangled = "_" + mangled
+ end if
+ ln += " asm(""" + mangled + """)"
+ end select
+#endif
+ '' ctor/dtor flags on prototypes
+ if( symbGetIsGlobalCtor( proc ) ) then
+ ln += " __attribute__((constructor))"
+ elseif( symbGetIsGlobalDtor( proc ) ) then
+ ln += " __attribute__((destructor))"
+ end if
+ end if
+
function = ln
end function
@@ -559,15 +603,7 @@ private sub hEmitFuncProto _
hWriteLine( "#define " & *symbGetMangledName( s ) & "( " & params & " ) " & _
"__builtin_" & *symbGetMangledName( s ) & "( " & params & " )", FALSE, TRUE )
else
- dim as string ln = hEmitProcHeader( s, EMITPROC_ISPROTO )
-
- if( symbGetIsGlobalCtor( s ) ) then
- ln += " __attribute__ ((constructor)) "
- elseif( symbGetIsGlobalDtor( s ) ) then
- ln += " __attribute__ ((destructor)) "
- end if
-
- hWriteLine( ln, TRUE )
+ hWriteLine( hEmitProcHeader( s, EMITPROC_ISPROTO ) )
end if
ctx.section = oldsection
@@ -2383,18 +2419,18 @@ private sub _emitProcBegin _
ctx.linenum = 0
end if
- dim as string ln
- if( symbIsExport( proc ) ) then
- ln += "__declspec(dllexport) "
- end if
-
- if( (proc->attrib and FB_SYMBATTRIB_NAKED) <> 0 ) then
- ln += "__attribute__ ((naked)) "
- end if
-
- ln += hEmitProcHeader( proc, 0 )
+#if 0
+ '' If the asm("mangledname") work-around is needed to tell gcc to not
+ '' add the @N suffix for stdcall procedures, emit an extra prototype
+ '' right above the procedure body, because asm() is only allowed on
+ '' prototypes.
+ select case( symbGetProcMode( proc ) )
+ case FB_FUNCMODE_STDCALL_MS, FB_FUNCMODE_PASCAL
+ hWriteLine( hEmitProcHeader( proc, EMITPROC_ISPROTO ) )
+ end select
+#endif
- hWriteLine( ln, FALSE, TRUE )
+ hWriteLine( hEmitProcHeader( proc, 0 ), FALSE, TRUE )
hWriteLine( "{", FALSE, TRUE )
ctx.identcnt += 1
View
29 todo.txt
@@ -1,8 +1,8 @@
[ ] add a C backend
- - MinGW gcc will always add the @N suffix on Stdcall procedures,
- thus STDCALL_MS and PASCAL procs will be mangled wrong on Win32.
- gcc doesn't seem to have any way to change this, only ld has
- --enable-stdcall-fixup (dangerous) and --kill-at (global, not per-function)
+ - MinGW gcc doesn't have STDCALL_MS or PASCAL; we could use 'asm("<alias>")' to
+ rename symbols, however that doesn't work with DLL export tables:
+ gcc inserts the full <alias> into '.ascii " -export:<alias>"', without stripping
+ the leading underscore, causing ld errors
[x] address of labels (used by the error trapping) must be emitted with 2 "&"'s
[x] the forward typedefs can't have a temp name or the g++ mangling will be wrong
[x] functions that return structs -- ABI-dependent, because the non-trivial UDT must be handled by FB
@@ -178,9 +178,6 @@
- must support RETURN or FUNCTION = to set the result
- must check the params if passed byval and if they have the right types
-[X] add the ... subscript when initializing array so
- "dim array(0 to ...) = {1, 2, 3, 4}" could be allowed
-
[ ] forward type defs used in byval parms of function ptr type defs shouldn't be an error
- the prototype mangling must be updated when that occurs, but how to do that fast
enough? the args would have to be linked to parent (the prototype), but updating
@@ -194,8 +191,6 @@
side type
- fix the !!!FIXME!!!'s and write the !!!WRITEME!!!'s in the rtlib
-[ ] swap of zstring's and var-len string's is not working?? or that's the behaviour..
-
[ ] passing a temporary string to a zstring ptr should not make yet-another temporary
assignment in hStrParamToPtrArg(), just a pointer copy that later should be
strDelete()'d
@@ -207,17 +202,17 @@
- := must be a new token because the "foo bar : baz" ambiguity
*** *** *** *** ***
-[ ] all functions returning STRING should actually return the FBSTRING
- object, but we can't code the it in g++, or in Linux we will need to
- link (and include) the stdc++ lib, that's over 2.5MB big, so it must
- be done in pure C, using the G++ ABI (ie: FBSTRING has a dtor, so it's
- never returned or passed by value):
+[ ] All functions returning STRING should actually return the FBSTRING object
+ - it must be coded in plain C to avoid C++ dependencies
- compiler has to allocate the descriptor as it does now following the gcc ABI
- any function in the run-time library returning strings will have to be
modified (chicken-egg problem)
- - no more run-time allocation of temp descriptors (better with threads)
- - no more STR_LOCK's (ditto)
- - str_delete won't check for temp descriptors anymore
+ - allocated on stack instead of using temp descriptors,
+ - better with threads, as no more locking needed in the rtlib
+ - allows STRING results to be passed between multiple rtlibs without
+ memory leaks (e.g. returned from DLL)
+ - no more STR_LOCK's
+ - no more checks for temp descriptors in all rtlib procs taking STRINGs
[ ] fixed-len strings compatible with QB:
- no null-term, temporaries always created when passing to functions
Please sign in to comment.
Something went wrong with that request. Please try again.