www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4059] New: Incorrect C++ name mangling

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059

           Summary: Incorrect C++ name mangling
           Product: D
           Version: 2.041
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: robert octarineparrot.com


--- Comment #0 from Robert Clipsham <robert octarineparrot.com> 2010-04-03
22:08:14 BST ---
b.cpp:
----
struct elem { };
void foobar(elem*, elem*) {}
----
a.d:
----
struct elem { }
extern(C++)void foobar(elem*, elem*);
void main(){
        elem *a;
        foobar(a, a);
}
----
Compile with:
$ gcc -c b.cpp -ob.o
$ dmd a.d b.o
This gives linking errors, as dmd does not mangle foobar properly. According to
nm, the correct mangle (found in b.cpp) is:
_Z6foobarP4elem
But dmd mangles it as:
_Z6foobarP4elemS_

Again, this is a blocker for ddmd on linux.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 03 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Ellery Newcomer <ellery-newcomer utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ellery-newcomer utulsa.edu


--- Comment #1 from Ellery Newcomer <ellery-newcomer utulsa.edu> 2010-04-03
14:58:02 PDT ---
What version of gcc do you use? with version 4.4.3, I get

_Z6foobarP4elemS0_

from nm b.o

(as well as a __gxx_personality_v0, which causes more problems)

and dmd 1.058 doesn't seem to want to mangle foobar at all.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059



--- Comment #2 from Robert Clipsham <robert octarineparrot.com> 2010-04-03
23:01:50 BST ---
I'm using gcc 4.4.3 too, and dmd 1.x won't mangle it as dmd 1.x does not
support C++ name mangling. Even with the mangle you give it doesn't match what
dmd outputs.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Koroskin Denis <2korden gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |2korden gmail.com
           Severity|normal                      |blocker


--- Comment #3 from Koroskin Denis <2korden gmail.com> 2010-04-12 06:02:09 PDT
---
Rising the severity as it is indeed a blocker

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 12 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Robert Clipsham <robert octarineparrot.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #4 from Robert Clipsham <robert octarineparrot.com> 2010-04-23
22:01:13 BST ---
The following patch seems to fix the issue, I haven't tested to see if this
breaks mangling in other cases though:

--- cppmangle.c 2010-03-18 18:58:06.000000000 +0000
+++ cppmangle.c 2010-04-23 21:59:47.000000000 +0100
   -69,11 +69,10   
     {
        if (p == components.data[i])
        {
-           /* Sequence is S_, S0_, .., S9_, SA_, ..., SZ_, S10_, ...
+           /* Sequence is S0_, .., S9_, SA_, ..., SZ_, S10_, ...
             */
            buf->writeByte('S');
-           if (i)
-               writeBase36(buf, i - 1);
+           writeBase36(buf, i);
            buf->writeByte('_');
            return 1;
        }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 23 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Robert Clipsham <robert octarineparrot.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|patch                       |


--- Comment #5 from Robert Clipsham <robert octarineparrot.com> 2010-04-24
19:24:44 BST ---
It seems there are other cases that this patch breaks, so there's obviously a
different way of fixing this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 24 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Lukasz Wrzosek <luk.wrzosek gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |luk.wrzosek gmail.com


--- Comment #6 from Lukasz Wrzosek <luk.wrzosek gmail.com> 2010-11-03 21:24:34
PDT ---
In DMD complex types 'save-points' are created in wrong order. This patch
should fix the issue.
I believe also that there is much more work needed in c++ mangling. The is no
easy way to call external C++ function from DMD when one of its arguments types
is long. The equivalent of long from C/C++ in D is int, but it is being mangled
differently, so cannot be linked after compilation faze.



Index: cppmangle.c
===================================================================
--- cppmangle.c    (wersja 737)
+++ cppmangle.c    (kopia robocza)
   -43,6 +43,10   
     static Array components;

     int substitute(OutBuffer *buf, void *p);
+
+    int exist(void *p);
+
+    void store(void *p);
 };

 Array CppMangleState::components;
   -82,6 +86,23   
     return 0;
 }

+int CppMangleState::exist(void *p)
+{
+    for (size_t i = 0; i < components.dim; i++)
+    {
+        if (p == components.data[i])
+        {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+void CppMangleState::store(void *p)
+{
+    components.push(p);
+}
+
 void source_name(OutBuffer *buf, Dsymbol *s)
 {
     char *name = s->ident->toChars();
   -266,19 +287,25   

 void TypePointer::toCppMangle(OutBuffer *buf, CppMangleState *cms)
 {
-    if (!cms->substitute(buf, this))
+    if (!cms->exist(this))
     {   buf->writeByte('P');
         next->toCppMangle(buf, cms);
+        cms->store(this);
     }
+    else
+        cms->substitute(buf, this);
 }


 void TypeReference::toCppMangle(OutBuffer *buf, CppMangleState *cms)
 {
-    if (!cms->substitute(buf, this))
+    if (!cms->exist(this))
     {   buf->writeByte('R');
         next->toCppMangle(buf, cms);
+        cms->store(this);
     }
+    else
+        cms->substitute(buf, this);
 }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Jacob Carlborg <doob me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob me.com


--- Comment #7 from Jacob Carlborg <doob me.com> 2010-11-04 02:02:34 PDT ---
(In reply to comment #6)
 In DMD complex types 'save-points' are created in wrong order. This patch
 should fix the issue.
 I believe also that there is much more work needed in c++ mangling. The is no
 easy way to call external C++ function from DMD when one of its arguments types
 is long. The equivalent of long from C/C++ in D is int, but it is being mangled
 differently, so cannot be linked after compilation faze.
The size of long in C/C++ depends on if the system is 64bit or not and what data model is used. To make the explanation short: On Windows long is 32 bits and on Unix-like systems long is 64 bits. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 04 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Jacob Carlborg <doob me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob me.com


--- Comment #7 from Jacob Carlborg <doob me.com> 2010-11-04 02:02:34 PDT ---
(In reply to comment #6)
 In DMD complex types 'save-points' are created in wrong order. This patch
 should fix the issue.
 I believe also that there is much more work needed in c++ mangling. The is no
 easy way to call external C++ function from DMD when one of its arguments types
 is long. The equivalent of long from C/C++ in D is int, but it is being mangled
 differently, so cannot be linked after compilation faze.
The size of long in C/C++ depends on if the system is 64bit or not and what data model is used. To make the explanation short: On Windows long is 32 bits and on Unix-like systems long is 64 bits. --- Comment #8 from Lukasz Wrzosek <luk.wrzosek gmail.com> 2010-11-08 08:44:55 PST --- AFAIK it is not so general. On my Linux 32bit system long is 32bit. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 08 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059



--- Comment #9 from Lukasz Wrzosek <luk.wrzosek gmail.com> 2010-11-08 08:50:17
PST ---
Created an attachment (id=805)
Diff

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 08 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059



--- Comment #10 from Jacob Carlborg <doob me.com> 2010-11-08 08:53:43 PST ---
(In reply to comment #8)
 AFAIK it is not so general. On my Linux 32bit system long is 32bit.
Maybe I was unclear but I was referring to 64bit systems. Lets try it once more: 32bit: Windows - "long" will be 32 bits long Unix-like - "long" will be 32 bits long 64bit: Windows - "long" will be 32 bits long Unix-like - "long" will be 64 bits long Again, this is simplified explanation as can be seen in the reference below. For reference: http://en.wikipedia.org/wiki/64-bit - The header is "Specific C-language data models" -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 08 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4059


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #11 from Walter Bright <bugzilla digitalmars.com> 2010-11-10
01:15:43 PST ---
http://www.dsource.org/projects/dmd/changeset/746

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 10 2010