From: burgers@ecn.nl (Teun Burgers)
Subject: errors in function return-values with __stdcall
12 Mar 1998 04:37:00 -0800
Message-ID: <199803120750.AA18778.cygnus.gnu-win32@joost.ecn.nl>
Mime-Version: 1.0
Content-Type: multipart/mixed;boundary=50e6_7d74-53c0_1057-1af4_ca7
Content-Transfer-Encoding: 7BIT
To: gnu-win32@cygnus.com
Cc: tvoverbe@wk.estec.esa.nl, jarvi@ezlink.com, stanton@haas.berkeley.edu


--50e6_7d74-53c0_1057-1af4_ca7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-MD5: pCNxCObQw9LZaeyX8qhddQ==
X-Sun-Data-Type: text

I think I found a problem when using __stdcall. In some cases function
returning double or float return the wrong values (printf prints
-1.#IND).  Within the function the return values are OK.  Double and
float arguments are passed correctly to functions.

I have attached a minimal program and Makefile.  The makefile makes 2
statically linked executables:  exe.exe has the standard default
C-calling convention, exe2.exe the Pascal calling convention.
exe.exe works OK, exe2.exe shows the problem.

I use mingw32 with Jan Jaaps gcc 2.8.1 and
GNU ld version 2.8.2 (with BFD 980119).

This problem turned up while trying to make DLL's for the Java Native Interface and
for MS-Excel (but can be reproduced with the statically linked example here).

I have the following questions.
- Am I doing something wrong?
- If not: how to solve/work around this problem

Thanks for any help

Teun Burgers

attachments:

exe.c: main program
hello.c: functions
hello.h: function prototypes
Makefile: Makefile

Drs A.R. Burgers        Netherlands Energy Research Foundation ECN
Phone: +31-224-564703   Renewable Energy, PV Cells & Modules
Fax  : +31-224-563214   P.O. Box 1
email: burgers@ecn.nl   1755 ZG Petten, The Netherlands			

--50e6_7d74-53c0_1057-1af4_ca7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-MD5: hYKWneW4haojBxnRI7c23A==
Content-Description: exe.c
X-Sun-Data-Type: c-file

#include "hello.h"
#include <stdio.h>

int main () {
	double x = 10,y=20,z;
	float xf = 10,yf=20,zf;
	int i = 10,j=20,k;
	k = helloi(i,j);
	printf("k after call of helloi: %d\n",k);
	z = helloddd(x,y);
	printf("z after call of helloddd: %g\n",z);
	zf = hellofff(xf,yf);
	printf("zf after call of hellofff: %hg\n",zf);
	z = hellodd(x);
	printf("z after call of hellodd: %g\n",z);
	z = hellod();
	printf("z after call of hellod: %g\n",z);
	return 0;
}
--50e6_7d74-53c0_1057-1af4_ca7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-MD5: Ij+G/ihdkddn0i47/nRM7A==
Content-Description: hello.c
X-Sun-Data-Type: c-file

#include "hello.h"
#include <stdio.h>

int CALLCONV helloi(int i, int j) 
{
	int k = i+j;
	printf("helloi: i,j on entry %d, %d\n",i,j);
	printf("helloi: k before exit %d\n",k);
	return k;
}

double CALLCONV helloddd (double x, double y)
{
	double z;
	printf("helloddd: x,y on entry %g, %g\n",x,y);
	z = x*y;
	printf("helloddd: zz before exit %g\n",z);
	return z;
}

float CALLCONV hellofff (float x, float y)
{
	float z;
	printf("hellofff: x,y on entry %hg, %hg\n",x,y);
	z = x*y;
	printf("hellofff: zz before exit %hg\n",z);
	return z;
}

double CALLCONV hellodd (double x)
{
	double z=x*x;
	printf("hellodd: z before exit %g\n",z);
	return z;
}

double CALLCONV hellod (void)
{
	double z=2;
	printf("hellod: z before exit %g\n",z);
	return z;
}
--50e6_7d74-53c0_1057-1af4_ca7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-MD5: JRICF3VRFo8Ggr0+8LgsVA==
Content-Description: hello.h
X-Sun-Data-Type: h-file

#ifdef PASCAL
#define CALLCONV __stdcall
#else
#define CALLCONV
#endif PASCAL

int CALLCONV helloi (int, int);

double  CALLCONV helloddd (double, double);

float CALLCONV hellofff (float x, float y);

double  CALLCONV hellodd (double);

double  CALLCONV hellod (void);
--50e6_7d74-53c0_1057-1af4_ca7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-MD5: aTckjau225Xtp7q4HTkHkg==
Content-Description: Makefile
X-Sun-Data-Type: Makefile

CC=gcc
all: exe.exe exe2.exe

exe.o exe2.o: exe.c hello.h
	$(CC) -c exe.c
	$(CC) -DPASCAL -o exe2.o -c exe.c

hello.o hello2.o: hello.c hello.h
	$(CC) -c hello.c
	$(CC) -DPASCAL -o hello2.o -c hello.c

exe.exe: exe.o hello.o
	$(CC) -o exe.exe exe.o hello.o

exe2.exe: exe.o hello2.o
	$(CC) -o exe2.exe exe2.o hello2.o

clean::
	-rm exe.exe exe2.exe hello.o hello2.o exe2.o exe.o
--50e6_7d74-53c0_1057-1af4_ca7--
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".
