K
Keith Thompson
jacob navia said:Look Heathfield, if you can't read it's nobody problem but yours;
As usual, you choose to be insulting just because you're talking to
Richard Heathfield.
It's a 258-page document. Do you expect him, or anyone else, to read
the whole thing just to answer a question? Sheesh.
Take for instance this C code. I add line numbers for reference
(1) int heathfield(short cbfalconer,double jkuyper)
(2) {
(3) }
I presume that doesn't appear in the ABI.
Here we have in line 1 a function that returns an integer
and receives two arguments: a short and a double.
Now, in the *real* world, we have to specify in which machine
register the argument is returned, and in which machine
register the arguments are passed if at all.
The C standard doesn't mention at all this, for obvious reasons.
An ABI then, is a standard thet allows programs compiled by
different vendors and even in different languages to cooperate
by means of a common interface.
The ABI mentioned above specifies how are arguments passed in
machine code to the function, and how the result is stored
when the function exits.
In line 2 we have an opening brace, i.e. a function prologue.
This is specified too, as well as line 3, the function epilogue.
So the ABI specifies how C code interfaces with other code, and that
certainly affects the generated code in some circumstances. But
that's nowhere near what the OP was asking about, namely "standards
that tell us how c code has to be compiled into machine code".
The ABI presumably says that the first argument, if it's of certain
types, is passed by copying it to some specified register (for
example; I don't know the details). That specifies the *effect* of
the generated code. My guess is that it doesn't mandate any
particular machine instructions to accomplish that effect. I would
expect that two compilers that achieve the same effect using different
instruction sequences would both satisfy the ABI's requirements.
Perhaps I've guessed incorrectly. (I'm not going to take the time to
download and read a 258-page document.) Perhaps, for some reason, it
really does mandate specific instruction sequences (if so, it would be
difficult to detect an implementation that violates such a requirement
by using a different instruction sequence to achieve the same effect).
But even if that's the case, I doubt that the ABI says anything about
*internal* code, i.e., for C code that doesn't interact with anything
outside the program.
You presented an example of a function that takes two arguments and
does nothing with them, an example specifically restricted to what the
ABI covers. What about something like this?
unsigned long factorial(unsigned long n)
{
if (n <= 1) return n;
return n * factorial(n - 1);
}
Assume that the compiler can determine that this function isn't called
from outside the compilation unit that contains it.
The ABI specifies how the function is to be called, how the argument
is to be passed to it, and how the returned value is to be made
available to the caller, but does it say *anything* else about the
machine code generated for this function? Does it specify whether
"n - 1" is computed using a subtraction or a decrement instruction?
Does it either require or forbid tail recursion optimization? Does it
require "n <= 1" to be computed using an integer comparison operator,
or can the compiler generate code to evaluate "!(n & ~1)"?
An ABI specifies how generated code interfaces with other code. It
*might* specify the actual generated machine code to accomplish this,
though I don't see why it would need to do so. It *doesn't* specify
in general what machine code is generated for C source code, which is
what the OP was asking about.
Yeah, C can be interpreted by Heathfield and co.
Instead of buying a computer, I can send my program to
Heathfield, he will parse it, and then
calculate everything with paper and pencil.
You don't do sarcasm well.
C can be compiled into some intermediate form and interpreted. That's
not a common implementation strategy, but it's perfectly legal.
There are as many standards as operating systems and processors.
For combination of OS and processor, there is a standard.
For instance for the Solaris operating system, there is an ABI for
the x86/32 bits, SPARC 32 bits, Sparc 64 bits, and x86/64 bits.
Obviously all the assembly examples in the ABI are not machine code.
For instance in page 40 we have
epilogue:
popl %ebx /restore local register
popl %esi /restore local register
popl %edi /restore local register
leave /restore framepointer
ret /pop return address
This is not machine code since if I search for
"machine code" without bothering to read
the document I will not find it
Are you suggesting that unless Richard takes the time to sit down and
read the entire 258-page document, he shouldn't comment on it?
Somebody else made the extraordinary claim (or at least implied it)
that this document specifies how to generate machine code from C.
It's up to the person making that claim, or to those supporting the
claim, to justify it.
Yes, not difficult to "set you straight". Learn to read.
Good advice. Please follow it.
This is the way those standards are done. We are not speaking about ISO
here, nor about C89...
Nor are we speaking about standards for generating machine code, which
was the original question.
The answer to the original question is "No.". There may be some
standards that specify some aspects of the generated machine code for
some systems.