finally move buildroot-ng to trunk
[openwrt.git] / toolchain / gdb / patches / 700-debian_cp-pass-by-reference.patch
1 This patch needs to be submitted for the FSF.  Also, there may be testcases
2 already in the GDB testsuite (currently disabled) that it would probably fix.
3
4 Index: gdb-6.3/gdb/infcall.c
5 ===================================================================
6 --- gdb-6.3.orig/gdb/infcall.c  2004-10-08 04:15:56.000000000 -0400
7 +++ gdb-6.3/gdb/infcall.c       2004-11-10 12:30:07.000000000 -0500
8 @@ -36,6 +36,7 @@
9  #include "gdb_string.h"
10  #include "infcall.h"
11  #include "dummy-frame.h"
12 +#include "cp-abi.h"
13  
14  /* NOTE: cagney/2003-04-16: What's the future of this code?
15  
16 @@ -297,8 +298,8 @@ call_function_by_hand (struct value *fun
17  {
18    CORE_ADDR sp;
19    CORE_ADDR dummy_addr;
20 -  struct type *value_type;
21 -  unsigned char struct_return;
22 +  struct type *value_type, *target_value_type;
23 +  unsigned char struct_return = 0, cp_struct_return = 0;
24    CORE_ADDR struct_addr = 0;
25    struct regcache *retbuf;
26    struct cleanup *retbuf_cleanup;
27 @@ -312,6 +313,7 @@ call_function_by_hand (struct value *fun
28    struct regcache *caller_regcache;
29    struct cleanup *caller_regcache_cleanup;
30    struct frame_id dummy_id;
31 +  struct cleanup *args_cleanup;
32  
33    if (!target_has_execution)
34      noprocess ();
35 @@ -410,10 +412,31 @@ call_function_by_hand (struct value *fun
36      using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
37    }
38  
39 -  /* Are we returning a value using a structure return or a normal
40 -     value return? */
41 +  /* Are we returning a value using a structure return (passing a
42 +     hidden argument pointing to storage) or a normal value return?
43 +     There are two cases: C++ ABI mandated structure return and
44 +     target ABI structure return.  The variable STRUCT_RETURN only
45 +     describes the latter.  The C++ version is handled by passing
46 +     the return location as the first parameter to the function,
47 +     even preceding "this".  This is different from the target
48 +     ABI version, which is target-specific; for instance, on ia64
49 +     the first argument is passed in out0 but the hidden structure
50 +     return pointer would normally be passed in r8.  */
51  
52 -  struct_return = using_struct_return (value_type, using_gcc);
53 +  if (current_language->la_language == language_cplus
54 +      && cp_pass_by_reference (value_type))
55 +    {
56 +      cp_struct_return = 1;
57 +
58 +      /* Tell the target specific argument pushing routine not to
59 +        expect a value.  */
60 +      target_value_type = builtin_type_void;
61 +    }
62 +  else
63 +    {
64 +      struct_return = using_struct_return (value_type, using_gcc);
65 +      target_value_type = value_type;
66 +    }
67  
68    /* Determine the location of the breakpoint (and possibly other
69       stuff) that the called function will return to.  The SPARC, for a
70 @@ -432,7 +455,7 @@ call_function_by_hand (struct value *fun
71        if (INNER_THAN (1, 2))
72         {
73           sp = push_dummy_code (current_gdbarch, sp, funaddr,
74 -                               using_gcc, args, nargs, value_type,
75 +                               using_gcc, args, nargs, target_value_type,
76                                 &real_pc, &bp_addr);
77           dummy_addr = sp;
78         }
79 @@ -440,7 +463,7 @@ call_function_by_hand (struct value *fun
80         {
81           dummy_addr = sp;
82           sp = push_dummy_code (current_gdbarch, sp, funaddr,
83 -                               using_gcc, args, nargs, value_type,
84 +                               using_gcc, args, nargs, target_value_type,
85                                 &real_pc, &bp_addr);
86         }
87        break;
88 @@ -507,9 +530,15 @@ call_function_by_hand (struct value *fun
89           param_type = TYPE_FIELD_TYPE (ftype, i);
90         else
91           param_type = NULL;
92 -       
93 +
94         args[i] = value_arg_coerce (args[i], param_type, prototyped);
95  
96 +       /* FIXME: Is current_language the right language?  */
97 +       if (current_language->la_language == language_cplus
98 +           && param_type != NULL
99 +           && cp_pass_by_reference (param_type))
100 +         args[i] = value_addr (args[i]);
101 +
102         /* elz: this code is to handle the case in which the function
103            to be called has a pointer to function as parameter and the
104            corresponding actual argument is the address of a function
105 @@ -607,7 +636,7 @@ You must use a pointer to function type 
106       stack, if necessary.  Make certain that the value is correctly
107       aligned. */
108  
109 -  if (struct_return)
110 +  if (struct_return || cp_struct_return)
111      {
112        int len = TYPE_LENGTH (value_type);
113        if (INNER_THAN (1, 2))
114 @@ -632,6 +661,22 @@ You must use a pointer to function type 
115         }
116      }
117  
118 +  if (cp_struct_return)
119 +    {
120 +      struct value **new_args;
121 +
122 +      /* Add the new argument to the front of the argument list.  */
123 +      new_args = xmalloc (sizeof (struct value *) * (nargs + 1));
124 +      new_args[0] = value_from_pointer (lookup_pointer_type (value_type),
125 +                                       struct_addr);
126 +      memcpy (&new_args[1], &args[0], sizeof (struct value *) * nargs);
127 +      args = new_args;
128 +      nargs++;
129 +      args_cleanup = make_cleanup (xfree, args);
130 +    }
131 +  else
132 +    args_cleanup = make_cleanup (null_cleanup, NULL);
133 +
134    /* Create the dummy stack frame.  Pass in the call dummy address as,
135       presumably, the ABI code knows where, in the call dummy, the
136       return address should be pointed.  */
137 @@ -649,6 +694,8 @@ You must use a pointer to function type 
138    else
139      error ("This target does not support function calls");
140  
141 +  do_cleanups (args_cleanup);
142 +
143    /* Set up a frame ID for the dummy frame so we can pass it to
144       set_momentary_breakpoint.  We need to give the breakpoint a frame
145       ID so that the breakpoint code can correctly re-identify the
146 @@ -839,11 +886,7 @@ the function call).", name);
147    /* Figure out the value returned by the function, return that.  */
148    {
149      struct value *retval;
150 -    if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
151 -      /* If the function returns void, don't bother fetching the
152 -        return value.  */
153 -      retval = allocate_value (value_type);
154 -    else if (struct_return)
155 +    if (struct_return || cp_struct_return)
156        /* NOTE: cagney/2003-09-27: This assumes that PUSH_DUMMY_CALL
157          has correctly stored STRUCT_ADDR in the target.  In the past
158          that hasn't been the case, the old MIPS PUSH_ARGUMENTS
159 @@ -853,6 +896,10 @@ the function call).", name);
160          "struct return convention", check that PUSH_DUMMY_CALL isn't
161          playing tricks.  */
162        retval = value_at (value_type, struct_addr, NULL);
163 +    else if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
164 +      /* If the function returns void, don't bother fetching the
165 +        return value.  */
166 +      retval = allocate_value (value_type);
167      else
168        {
169         /* This code only handles "register convention".  */
170 Index: gdb-6.3/gdb/cp-abi.h
171 ===================================================================
172 --- gdb-6.3.orig/gdb/cp-abi.h   2003-04-12 13:41:25.000000000 -0400
173 +++ gdb-6.3/gdb/cp-abi.h        2004-11-10 12:30:07.000000000 -0500
174 @@ -1,7 +1,7 @@
175  /* Abstraction of various C++ ABI's we support, and the info we need
176     to get from them.
177     Contributed by Daniel Berlin <dberlin@redhat.com>
178 -   Copyright 2001 Free Software Foundation, Inc.
179 +   Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
180  
181     This file is part of GDB.
182  
183 @@ -145,6 +145,10 @@ extern struct type *value_rtti_type (str
184  extern int baseclass_offset (struct type *type, int index, char *valaddr,
185                              CORE_ADDR address);
186                    
187 +/* Return non-zero if an argument of type TYPE should be passed by reference
188 +   instead of value.  */
189 +extern int cp_pass_by_reference (struct type *type);
190 +
191  struct cp_abi_ops
192  {
193    const char *shortname;
194 @@ -162,6 +166,7 @@ struct cp_abi_ops
195                              int *using_enc);
196    int (*baseclass_offset) (struct type *type, int index, char *valaddr,
197                            CORE_ADDR address);
198 +  int (*pass_by_reference) (struct type *type);
199  };
200  
201  
202 Index: gdb-6.3/gdb/cp-abi.c
203 ===================================================================
204 --- gdb-6.3.orig/gdb/cp-abi.c   2003-11-26 17:04:00.000000000 -0500
205 +++ gdb-6.3/gdb/cp-abi.c        2004-11-10 12:30:07.000000000 -0500
206 @@ -1,5 +1,5 @@
207  /* Generic code for supporting multiple C++ ABI's
208 -   Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
209 +   Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
210  
211     This file is part of GDB.
212  
213 @@ -94,6 +94,14 @@ value_rtti_type (struct value *v, int *f
214    return (*current_cp_abi.rtti_type) (v, full, top, using_enc);
215  }
216  
217 +int
218 +cp_pass_by_reference (struct type *type)
219 +{
220 +  if ((current_cp_abi.pass_by_reference) == NULL)
221 +    return 0;
222 +  return (*current_cp_abi.pass_by_reference) (type);
223 +}
224 +
225  /* Set the current C++ ABI to SHORT_NAME.  */
226  
227  static int
228 Index: gdb-6.3/gdb/gnu-v3-abi.c
229 ===================================================================
230 --- gdb-6.3.orig/gdb/gnu-v3-abi.c       2004-03-15 15:38:08.000000000 -0500
231 +++ gdb-6.3/gdb/gnu-v3-abi.c    2004-11-10 12:30:07.000000000 -0500
232 @@ -1,7 +1,7 @@
233  /* Abstraction of GNU v3 abi.
234     Contributed by Jim Blandy <jimb@redhat.com>
235  
236 -   Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
237 +   Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
238  
239     This file is part of GDB.
240  
241 @@ -419,6 +419,84 @@ gnuv3_baseclass_offset (struct type *typ
242    return base_offset;
243  }
244  
245 +/* Return nonzero if a type should be passed by reference.
246 +
247 +   The rule in the v3 ABI document comes from section 3.1.1.  If the
248 +   type has a non-trivial copy constructor or destructor, then the
249 +   caller must make a copy (by calling the copy constructor if there
250 +   is one or perform the copy itself otherwise), pass the address of
251 +   the copy, and then destroy the temporary (if necessary).
252 +
253 +   For return values with non-trivial copy constructors or
254 +   destructors, space will be allocated in the caller, and a pointer
255 +   will be passed as the first argument (preceding "this").
256 +
257 +   We don't have a bulletproof mechanism for determining whether a
258 +   constructor or destructor is trivial.  For GCC and DWARF2 debug
259 +   information, we can check the artificial flag.
260 +
261 +   We don't do anything with the constructors or destructors yet,
262 +   but we have to get the argument passing right anyway.  */
263 +static int
264 +gnuv3_pass_by_reference (struct type *type)
265 +{
266 +  int fieldnum, fieldelem, basenum;
267 +
268 +  CHECK_TYPEDEF (type);
269 +
270 +  /* We're only interested in things that can have methods.  */
271 +  if (TYPE_CODE (type) != TYPE_CODE_STRUCT
272 +      && TYPE_CODE (type) != TYPE_CODE_CLASS
273 +      && TYPE_CODE (type) != TYPE_CODE_UNION)
274 +    return 0;
275 +
276 +  for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
277 +    for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
278 +        fieldelem++)
279 +      {
280 +       struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
281 +       char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
282 +       struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
283 +
284 +       /* If this function is marked as artificial, it is compiler-generated,
285 +          and we assume it is trivial.  */
286 +       if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
287 +         continue;
288 +
289 +       /* If we've found a destructor, we must pass this by reference.  */
290 +       if (name[0] == '~')
291 +         return 1;
292 +
293 +       /* If the mangled name of this method doesn't indicate that it
294 +          is a constructor, we're not interested.
295 +
296 +          FIXME drow/2004-05-27: We could do this using the name of
297 +          the method and the name of the class instead of dealing
298 +          with the mangled name.  We don't have a convenient function
299 +          to strip off both leading scope qualifiers and trailing
300 +          template arguments yet.  */
301 +       if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
302 +         continue;
303 +
304 +       /* If this method takes two arguments, and the second argument is
305 +          a reference to this class, then it is a copy constructor.  */
306 +       if (TYPE_NFIELDS (fieldtype) == 2
307 +           && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
308 +           && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type)
309 +         return 1;
310 +      }
311 +
312 +  /* Even if all the constructors and destructors were artificial, one
313 +     of them may have invoked a non-artificial constructor or
314 +     destructor in a base class.  If any base class needs to be passed
315 +     by reference, so does this class.  */
316 +  for (basenum = 0; basenum < TYPE_N_BASECLASSES (type); basenum++)
317 +    if (gnuv3_pass_by_reference (TYPE_BASECLASS (type, basenum)))
318 +      return 1;
319 +
320 +  return 0;
321 +}
322 +
323  static void
324  init_gnuv3_ops (void)
325  {
326 @@ -434,6 +512,7 @@ init_gnuv3_ops (void)
327    gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
328    gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
329    gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
330 +  gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
331  }
332  
333  extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
334 Index: gdb-6.3/gdb/hpacc-abi.c
335 ===================================================================
336 --- gdb-6.3.orig/gdb/hpacc-abi.c        2003-06-08 14:27:13.000000000 -0400
337 +++ gdb-6.3/gdb/hpacc-abi.c     2004-11-10 12:30:07.000000000 -0500
338 @@ -3,7 +3,7 @@
339     Most of the real code is from HP, i've just fiddled it to fit in
340     the C++ ABI abstraction framework.
341  
342 -   Copyright 2001 Free Software Foundation, Inc.
343 +   Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
344  
345     This file is part of GDB.
346  
347 Index: gdb-6.3/gdb/Makefile.in
348 ===================================================================
349 --- gdb-6.3.orig/gdb/Makefile.in        2004-11-10 12:30:06.000000000 -0500
350 +++ gdb-6.3/gdb/Makefile.in     2004-11-10 12:30:07.000000000 -0500
351 @@ -2073,7 +2073,7 @@ ia64-tdep.o: ia64-tdep.c $(defs_h) $(inf
352  infcall.o: infcall.c $(defs_h) $(breakpoint_h) $(target_h) $(regcache_h) \
353         $(inferior_h) $(gdb_assert_h) $(block_h) $(gdbcore_h) $(language_h) \
354         $(objfiles_h) $(gdbcmd_h) $(command_h) $(gdb_string_h) $(infcall_h) \
355 -       $(dummy_frame_h)
356 +       $(dummy_frame_h) $(cp_abi_h)
357  inf-child.o: inf-child.c $(defs_h) $(regcache_h) $(memattr_h) $(symtab_h) \
358         $(target_h) $(inferior_h) $(gdb_string_h)
359  infcmd.o: infcmd.c $(defs_h) $(gdb_string_h) $(symtab_h) $(gdbtypes_h) \
360 Index: gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.exp
361 ===================================================================
362 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
363 +++ gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.exp        2004-11-11 09:48:00.498518899 -0500
364 @@ -0,0 +1,38 @@
365 +# This testcase is part of GDB, the GNU debugger.
366 +
367 +# Copyright 2004 Free Software Foundation, Inc.
368 +
369 +# This program is free software; you can redistribute it and/or modify
370 +# it under the terms of the GNU General Public License as published by
371 +# the Free Software Foundation; either version 2 of the License, or
372 +# (at your option) any later version.
373 +#
374 +# This program is distributed in the hope that it will be useful,
375 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
376 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
377 +# GNU General Public License for more details.
378 +#
379 +# You should have received a copy of the GNU General Public License
380 +# along with this program; if not, write to the Free Software
381 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
382 +
383 +# Check that GDB can call C++ functions whose parameters have
384 +# object type, but are passed by reference.
385 +
386 +set testfile "pass-by-ref"
387 +set srcfile ${testfile}.cc
388 +set binfile ${objdir}/${subdir}/${testfile}
389 +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
390 +    return -1
391 +}
392 +
393 +gdb_exit
394 +gdb_start
395 +gdb_reinitialize_dir $srcdir/$subdir
396 +gdb_load ${binfile}
397 +
398 +if ![runto_main] then {
399 +    return -1
400 +}
401 +
402 +gdb_test "print foo (global_obj)" " = 3" "call function"
403 Index: gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.cc
404 ===================================================================
405 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
406 +++ gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.cc 2004-11-11 09:44:17.815014667 -0500
407 @@ -0,0 +1,57 @@
408 +/* This testcase is part of GDB, the GNU debugger.
409 +
410 +   Copyright 2004 Free Software Foundation, Inc.
411 +
412 +   This program is free software; you can redistribute it and/or modify
413 +   it under the terms of the GNU General Public License as published by
414 +   the Free Software Foundation; either version 2 of the License, or
415 +   (at your option) any later version.
416 +
417 +   This program is distributed in the hope that it will be useful,
418 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
419 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
420 +   GNU General Public License for more details.
421
422 +   You should have received a copy of the GNU General Public License
423 +   along with this program; if not, write to the Free Software
424 +   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
425 +   USA.  */
426 +
427 +class Obj {
428 +public:
429 +  Obj ();
430 +  Obj (const Obj &);
431 +  ~Obj ();
432 +  int var[2];
433 +};
434 +
435 +int foo (Obj arg)
436 +{
437 +  return arg.var[0] + arg.var[1];
438 +}
439 +
440 +Obj::Obj ()
441 +{
442 +  var[0] = 1;
443 +  var[1] = 2;
444 +}
445 +
446 +Obj::Obj (const Obj &obj)
447 +{
448 +  var[0] = obj.var[0];
449 +  var[1] = obj.var[1];
450 +}
451 +
452 +Obj::~Obj ()
453 +{
454 +
455 +}
456 +
457 +Obj global_obj;
458 +
459 +int
460 +main ()
461 +{
462 +  int bar = foo (global_obj);
463 +  return bar;
464 +}