[gnu.g++.bug] Pointer-to-virtual-function loser

bryan@relay.eu.net (Bryan Boreham) (10/28/89)

Program:


      class firble
      {
      public:
	int bar;
	virtual void Print();
      };

      class smartptr
      {
      public:
	int foo;
	firble* operator->();
      };


      int main()
      {
	typedef void (firble::*FPTR) ();

	FPTR x;
	smartptr y;

	(y->*x)();	// This is the interesting line.
	return 0;
      }


Compilation:

cc1plus test5.cc -quiet -dumpbase test5.cc -Wall -noreg -version -o test5.s
GNU C++ version 1.36.0- (based on GCC 1.36) (68k, MIT syntax) compiled by GNU C version 1.36.
default target switches: -m68020 -mc68020 -m68881 -mbitfield


Assembly: (for a Sun 3)

#NO_APP
gcc_compiled.:
.text
	.even
.globl _main
_main:
	link a6,#-8
	movel a2,sp@-
	jbsr ___main
	movel d0,d0
	tstl a6@(-4)
	jle L2     <------ test the high bit to see if this is virtual.
	movel a6@(-4),a2
	jra L3
L2:                <------ only get here if x points to a virtual fn.
	movel a6,d0
	subql #8,d0
	movel d0,sp@-
	jbsr _op$component__8smartptre
	addqw #4,sp
	movel d0,a0
	movel a6@(-4),d0
	asll #3,d0
	movel a0@(4),a1
	movel a1@(4,d0:l),a2
L3:
	movel a0,sp@-
	jbsr a2@
	clrl d0
	jra L1
	clrl d0
	jra L1
L1:
	movel a6@(-12),a2
	unlk a6
	rts


So the operator-> is only called if the pointer passed is to a virtual
function. This doesn't seem right, especially since a0 doesn't contain
anything interesting at L3 in the non-virtual case.

This happened in g++ 1.35 too. I hope it isn't a language feature.

Bryan Boreham			bryan@kewill.uucp  
Software Engineer	||	bryan%kewill@uunet.uu.net
Kewill Systems PLC	||  ... uunet!mcvax!ukc!root44!kewill!bryan
Walton-On-Thames	
Surrey, England		Telephone: (+44) 932 248 328