News for hackers. Stuff that matters.
Re:C, Fortran, and single-character strings by kazinator on Saturday June 22, 2019 @12:51AM (#20248085)
Hmm. Unless my understanding is off, these tail calls will work right if there happens to be a word in the stack where the missing argument is supposed to be, and that word has a value of 1 (correct string length). Because that looks indistinguishable from the correct argument having been passed.

So that suggests a run-time solution like this:

1. The function examines the string length argument word. If that word is 1, everything is cool; the function proceeds, the helper function is tail called and so forth.

2. If, on entry, that word is not 1, then the function calls itself (with a real non-tail call that allocates new argument). It passes itself the missing 1 value properly. When that call nested returns, it also returns.

3. This nested invocation issued in (2) now sees a value of 1 in that argument since it is correctly passed, and so it proceeds as given in step (1). When all that tail-call-ology finally executes a proper return somewhere, it will return to this nested invocation of that function, which will pop out, and return to the C caller, as described in (2).

No need for a compiler switch to turn off tail calls. Tail calls work among functions that obey the ABI (like all Fortran-Fortran calls). When an ABI violation is detected, then we get an extra frame. (Hopefully there aren't tail calling loops that involve cycling between Fortran and broken C.)

The broken C can be gradually fixed. The workaround can be deprecated and removed when that happens. Correct code doesn't trigger the workaround behavior in the invoked functions and also doesn't suffer the performance hit of the extra nested call. That creates an incentive to fix the C code.

Re:C, Fortran, and single-character strings by comex on Saturday June 22, 2019 @01:05AM (#20248124)
I’m not sure that doing that check in every function that might be called from C would actually improve performance compared to just turning off tail calls.
Re:C, Fortran, and single-character strings by kazinator on Saturday June 22, 2019 @02:17AM (#20248288)
Well, not every function that might be called from C; every function which takes this funny string argument that is known to be of length 1 that might be called from C.