[gnu.g++.bug] function called twice for stream & itor

eppstein@aldebaran.uucp (David Eppstein) (10/27/89)

I have a function that returns an (ostream &), that must be called
exactly once (not twice).  But when I say (ostream & o = fn()),
g++ (1.35 on a SPARC) generates code to call it twice (a bug).

            ------------ input code ------------
#include <stream.h>

ostream & bar();

foo()
{
    ostream & o = bar();
    o << "foo\n";
}

            ------------ generated assembly (SPARC) ------------
gcc_compiled.:
.text
LC0:
	.ascii "foo\12\0"
	.align 4
.global _foo
	.proc 1
_foo:
	!#PROLOGUE# 0
	save %sp,-144,%sp
	!#PROLOGUE# 1
	call _bar,0
	nop
	call _bar,0			// Here is the bug
	nop
	sethi %hi(LC0),%o1
	or %lo(LC0),%o1,%o1
	mov %o0,%l0
	mov 0,%l1
	ld [%l0+4],%o0
	tst %o0
	bne L544
	nop
	call _sputs_PSstreambuf_PQI,0
	ld [%l0],%o0
	cmp %o0,-1
	bne L543			// This code is correct, but strange.
	nop				// Why not "be (label after be L546)",
L544:					// and flush the "mov 1,%l1" stuff?
	mov 1,%l1
L543:
	tst %l1
	be L546
	nop
	ld [%l0+4],%o0
	or %o0,2,%o0
	st %o0,[%l0+4]
L546:
	ret
	restore

            ------------ output of g++ -v ------------
g++ version 1.35.1-
 /import/lib/gcc-cpp -+ -v -undef -D__GNU__ -D__GNUG__ -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__OPTIMIZE__ test.c /tmp/cca02522.cpp
GNU CPP version 1.36
 /import/lib/gcc-cc1plus /tmp/cca02522.cpp -quiet -dumpbase test.c -finline-functions -opt -version -o test.s
GNU C++ version 1.35.1- (sparc) compiled by GNU C version 1.35.