(j3.2006) Interesting F2003-ism
Andy Vaught
andyv
Fri Feb 23 12:03:41 EST 2007
On Fri, 23 Feb 2007, Malcolm Cohen wrote:
> Andy Vaught said of Michael's though experiment:
> > But you can't implement a C ABI that way.
>
> Possibly not, but there is actually quite a lot of leeway.
>
> > Variable argument lists have
> > to be passed in the same manner as fixed argument lists.
>
> You say that, but I am less than unconvinced.
>
> > Otherwise, a
> > variable argument list function won't work when invoked through a function
> > pointer that has been cast around a bit.
>
> And many of those cases are illegal according to the C standard.
>
> How far the few legal cases constrain how the ABI is forced to be
> implemented is not entirely clear. I've certainly heard of implementations
> that work similarly (not quite the same) to what Mike was suggesting.
> Of course there need not even be an ABI...
> What *is* entirely clear is that you have to call a function that is
> defined with an ellipsis with a prototype, and that prototype must
> use ellipsis the same as the function definition.
>
> This follows reasonably straightforwardly from p120 of c99.
>
> So your printf examples are just wrong.
>
> Whether gcc "lets them work" is a rather uninteresting question.
> We didn't add BIND(GCC), we added BIND(C).
>
> In fact my copy of gcc 3.4.4 doesn't allow it anyway:
> % cat junkz.c
> s1() { extern printf(char *,int); printf("%d",3); }
> s2() { extern printf(char *,float); printf("%e",3.5f); }
> % gcc junkz.c
> junkz.c: In function `s1':
> junkz.c:1: warning: conflicting types for built-in function 'printf'
> junkz.c: In function `s2':
> junkz.c:2: error: conflicting types for 'printf'
> junkz.c:1: error: previous declaration of 'printf' was here
And yet, after all this, if a C compiler sees something like:
void sub(void) {
foo(1, 2, 3); /* No prototype of foo. Variable or fixed? */
}
Somehow, it has to call foo(). It sounds like someone could make a
conforming C compiler that would compile this program into something that
would crash but no one will.
And footnote 125 on p99 of C99 is:
125) If both function types are "old style", parameter types are not
compared.
Probably not enough for you, but I'm going to allow people to do clever
things.
Andy
More information about the J3
mailing list