Samples of external code with various compilers?
On Tue, 3 Dec 2002, Duncan Murdoch wrote:
On Tue, 3 Dec 2002 10:01:08 +1100 , Mark.Bravington@csiro.au wrote:
what call mode to use for procedures (i.e. stack order and removal of parameters; I had always used STDCALL with S and R, and then found I was getting bugs with R 1.6.1. So in desperation I eventually changed this at random to C-CALL, and things started working again in R-- and continued to work in S. To my continued puzzlement, actually.)
The difference between stdcall and cdecl is that in the former, the routine removes parameters from the stack, whereas in the latter, the caller does. R uses cdecl. When the routine used stdcall, the parameters would be removed twice. My guess about why this worked was that the "do_dotCode" routine had enough redundant locals that having some of them removed from the stack didn't cause obvious problems. In 1.6, do_dotCode was modified, and now it messes up if you steal its locals. Most other Windows programs use stdcall. Using the stdcall convention to call a cdecl routine means that the parameters won't be removed from the stack by either the caller or the routine. However, S-PLUS is probably like R, and only makes one call to your routine from the function that calls it. When it returns, the extra junk on the stack is removed.
S-PLUS 2000 was like R. S-PLUS 6 is cleverer and tries to figure out (from the decoration on the symbol, I believe) if stdcall or cdecl is required, but it defaults to stdcall.
Since stdcall is the Windows standard, and R uses cdecl, I've been thinking lately about whether it would be worth putting in stdcall as an option to .C and .Fortran. Possibly we could just switch to stdcall, and rely on the behaviour in the paragraph above to handle cdecl routines, but that sounds pretty ugly to me.
You can't because of callbacks into R from the compiled code. That's where the problems arise with S-PLUS 6: such callbacks have to be stdcall there. stdcall requires that all the calls have the right number and type of parameters. That's really difficult to check with Fortran code (or with C code with incomplete headers). Working with S-PLUS 6 has been much more error-prone precisely because of the use of stdcall, and I don't see it as a way forward.
Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595