Bug 34079 - Bind(C): Character argument/return value problems
Bind(C): Character argument/return value problems
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.3.0
: P3 normal
: ---
Assigned To: Tobias Burnus
: patch, wrong-code
Depends on: 34137
Blocks: 32630 34246
  Show dependency treegraph
 
Reported: 2007-11-13 08:07 UTC by Tobias Burnus
Modified: 2007-11-25 22:18 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-11-15 15:10:33


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-11-13 08:07:29 UTC
Using STDCALL, not the callee but the called procedure pops the arguments from the stack. The problem is that gfortran currently also for BIND(C) passes the string lengths as arguments.

See also:
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/19d77dfc75f8be58
Comment 1 Tobias Burnus 2007-11-13 14:39:37 UTC
Example:

interface
  subroutine subiso(x) bind(c)
    use iso_c_binding
    character(kind=c_char,len=1), dimension(*) :: x
  end subroutine subiso
  subroutine subiso2(x) bind(c) ! { dg-warning "may not be C interoperable" }
    character(len=1), dimension(*) :: x
  end subroutine subiso2
  subroutine sub(x)
    use iso_c_binding
    character(kind=c_char,len=1), dimension(*) :: x
  end subroutine sub
end interface
call sub    ("abcdef")
call subiso ("ABCDEF")
call subiso2("AbCdEf")
end

Dumped tree should look like:
  sub (&"abcdef"[1]{lb: 1 sz: 1}, 6);
  subiso (&"ABCDEF"[1]{lb: 1 sz: 1});
  subiso2 (&"ABCDEF"[1]{lb: 1 sz: 1});


Draft of a patch:

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c    (Revision 130131)
+++ gcc/fortran/trans-expr.c    (Arbeitskopie)
@@ -2390,8 +2390,8 @@ gfc_conv_function_call (gfc_se * se, gfc
         }

       /* Character strings are passed as two parameters, a length and a
-         pointer.  */
-      if (parmse.string_length != NULL_TREE)
+         pointer - except for Bind(c) which only passes the pointer.  */
+      if (parmse.string_length != NULL_TREE && !sym->attr.is_bind_c)
         stringargs = gfc_chainon_list (stringargs, parmse.string_length);

       arglist = gfc_chainon_list (arglist, parmse.expr);
Comment 2 Tobias Burnus 2007-11-13 20:02:40 UTC
Note: The patch above works, one only needs to check whether there is a memory leak or something else which affects the generated tree.

Index: gcc/testsuite/gfortran.dg/bind_c_vars_2.f03
===================================================================
--- gcc/testsuite/gfortran.dg/bind_c_vars_2.f03 (revision 0)
+++ gcc/testsuite/gfortran.dg/bind_c_vars_2.f03 (revision 0)
@@ -0,0 +1,29 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! PR fortran/34079
+! Character bind(c) arguments shall not pass the length as additional argument
+!
+implicit none
+interface
+  subroutine subiso(x) bind(c)
+    use iso_c_binding
+    character(kind=c_char,len=1), dimension(*) :: x
+  end subroutine subiso
+  subroutine subiso2(x) bind(c) ! { dg-warning "may not be C interoperable" }
+    character(len=1), dimension(*) :: x
+  end subroutine subiso2
+  subroutine sub(x)
+    use iso_c_binding
+    character(kind=c_char,len=1), dimension(*) :: x
+  end subroutine sub
+end interface
+call sub    ("abc")
+call subiso ("ABCDEF")
+call subiso2("AbCdEfGhIj")
+end
+
+! { dg-final { scan-tree-dump-times "{lb: 1 sz: 1}, 3" 1 "original" } }
+! { dg-final { scan-tree-dump-times "{lb: 1 sz: 1}, 6" 0 "original" } }
+! { dg-final { scan-tree-dump-times "{lb: 1 sz: 1}, 10" 0 "original" } }
+! { dg-final { cleanup-tree-dump "original" } }
Comment 4 patchapp@dberlin.org 2007-11-15 15:10:40 UTC
Subject: Bug number PR34079

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-11/msg00851.html
Comment 5 Tobias Burnus 2007-11-17 08:08:18 UTC
Additional problems, spotted by FX:

subroutine foo(x) bind(c)
   character(len=1) :: x

function bar(x) bind(c)
   character(len=1) :: x, bar

These generates:
  foo (x, _x)
  bar (__result, .__result, x, _x)

Instead of
   void foo(char *x)
and
  char bar(char *x)
Comment 6 Tobias Burnus 2007-11-17 13:49:40 UTC
More fun with ENTRY:

a) gfortran does not match the BIND(C) suffix for ENTRY,
   "R1235 entry-stmt is ENTRY entry-name [ ( [ dummy-arg-list ] ) [ suffix ] ]"
   cf. decl.c: gfc_match_function_decl and gfc_match_entry

b) As entry and procedure do not need to be both bind(c), the master function should always be called with the string length.
Comment 7 Tobias Burnus 2007-11-19 12:30:31 UTC
Subject: Bug 34079

Author: burnus
Date: Mon Nov 19 12:30:17 2007
New Revision: 130288

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130288
Log:
2007-11-19  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * decl.c (gfc_match_entry): Support BIND(C).
        (gfc_match_subroutine): Fix comment typo.

2007-11-19  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * gfortran.dg/bind_c_usage_10_c.c: New.
        * gfortran.dg/bind_c_usage_10.f03: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_10.f03
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_10_c.c
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 8 patchapp@dberlin.org 2007-11-20 05:04:40 UTC
Subject: Bug number PR34079

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-11/msg00980.html
Comment 9 Tobias Burnus 2007-11-22 09:56:02 UTC
Subject: Bug 34079

Author: burnus
Date: Thu Nov 22 09:55:47 2007
New Revision: 130346

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130346
Log:
2007-11-22  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * trans-expr.c (gfc_conv_function_call): Do not append
        string length arguments when calling bind(c) procedures.
        * trans-decl.c (create_function_arglist): Do not append
        string length arguments when declaring bind(c) procedures.

2007-11-22  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * gfortran.dg/bind_c_usage_10.f03: Remove .mod file afterwards.
        * gfortran.dg/bind_c_usage_13.f03: New.
        * gfortran.dg/bind_c_usage_14.f03: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_13.f03
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_14.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_10.f03

Comment 10 patchapp@dberlin.org 2007-11-22 19:01:13 UTC
Subject: Bug number PR34079

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-11/msg01109.html
Comment 11 patchapp@dberlin.org 2007-11-24 13:05:32 UTC
Subject: Bug number PR34079

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-11/msg01275.html
Comment 12 Tobias Burnus 2007-11-25 22:03:02 UTC
Subject: Bug 34079

Author: burnus
Date: Sun Nov 25 22:02:53 2007
New Revision: 130414

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130414
Log:
2007-11-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * trans-types.c (gfc_return_by_reference,
        gfc_get_function_type): Do not return result of
        character-returning bind(C) functions as argument.
        * trans-expr.c (gfc_conv_function_call): Ditto.

2007-11-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * gfortran.dg/bind_c_usage_10_c.c: Fix comment.
        * gfortran.dg/bind_c_usage_16.f03: New.
        * gfortran.dg/bind_c_usage_16_c.c: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_16.f03
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_16_c.c
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-types.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/bind_c_usage_10_c.c

Comment 13 Tobias Burnus 2007-11-25 22:15:54 UTC
Subject: Bug 34079

Author: burnus
Date: Sun Nov 25 22:15:47 2007
New Revision: 130417

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130417
Log:
Forgot trans-expr.c in previous commit:

2007-11-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/34079
        * trans-types.c (gfc_return_by_reference,
        gfc_get_function_type): Do not return result of
        character-returning bind(C) functions as argument.
        * trans-expr.c (gfc_conv_function_call): Ditto.

Modified:
    trunk/gcc/fortran/trans-expr.c

Comment 14 Tobias Burnus 2007-11-25 22:18:49 UTC
FIXED on the trunk (4.3.0) [there is no bind(C) support in 4.2.x].