linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Value to subtract from ESP register?
@ 2004-03-24 16:22 zavandi
  2004-03-24 17:25 ` Chris Nanakos
  0 siblings, 1 reply; 4+ messages in thread
From: zavandi @ 2004-03-24 16:22 UTC (permalink / raw)
  To: linux-c-programming

I have a question which regards mostly gcc. If isn't appropriate here, I 
apologize, maybe someone can point me to a more appropriate mailing list.

So... the problem: When gcc compiles a function, the first thing the 
function does is to subtract a certain value from the ESP register, to 
make room for local variables. This value is always >= than the size of 
local variables, but I'd be curious to know what algorithm gcc uses to 
decide how much greater the value will be.

An example: I have this code in C:

---start------------------

void f1() {
          char d[3];
}

void f2() {
          char d[4];
}

---end--------------------

The assembly output for f1 and f2 functions is (gcc -S test.c):

---start------------------

f1:
          pushl   %ebp
          movl    %esp, %ebp
          subl    $24, %esp
          leave
          ret

f2:
          pushl   %ebp
          movl    %esp, %ebp
          subl    $4, %esp
          leave
          ret

---end--------------------

I would like to know why the subtracted value is 24 when the local 
variable is 3 bytes and 4 when the variable is 4 bytes.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Value to subtract from ESP register?
  2004-03-24 16:22 Value to subtract from ESP register? zavandi
@ 2004-03-24 17:25 ` Chris Nanakos
  2004-03-24 19:39   ` zavandi
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Nanakos @ 2004-03-24 17:25 UTC (permalink / raw)
  To: zavandi, linux-c-programming

This is the optimazation code that gcc uses.It differs from version to
version and you can discover it only if you work with different
arguments(values) in your arrays.After a step the subtraction of ESP is the
same e.g for 128 and so on.But i think that somewhere on the documentation
of gcc is already written.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Value to subtract from ESP register?
  2004-03-24 17:25 ` Chris Nanakos
@ 2004-03-24 19:39   ` zavandi
  2004-03-24 21:37     ` Glynn Clements
  0 siblings, 1 reply; 4+ messages in thread
From: zavandi @ 2004-03-24 19:39 UTC (permalink / raw)
  To: linux-c-programming

Chris Nanakos wrote:
> This is the optimazation code that gcc uses.It differs from version to
> version and you can discover it only if you work with different
> arguments(values) in your arrays.After a step the subtraction of ESP is the
> same e.g for 128 and so on.But i think that somewhere on the documentation
> of gcc is already written.

Thanks. I've looked in the gcc's documentation but I was unable to find 
the answer to my problem (or maybe I didn't look in the right 
documentation).

Anyway, about my specific example, how subtracting 24 instead of 4 is 
gonna optimize anything?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Value to subtract from ESP register?
  2004-03-24 19:39   ` zavandi
@ 2004-03-24 21:37     ` Glynn Clements
  0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2004-03-24 21:37 UTC (permalink / raw)
  To: linux-c-programming


zavandi wrote:

> Anyway, about my specific example, how subtracting 24 instead of 4 is 
> gonna optimize anything?

In most cases, gcc rounds the size up to keep the stack pointer
aligned to a 16-byte boundary. If you were to generate functions for
1-byte, 2-byte, 3-byte etc arrays you would note that for arrays of
1-16 bytes (with certain exceptions; see below), it subtracts 24; for
17-32 bytes, it subtracts 40; for 33-48 bytes, it subtracts 56, etc
(the pushed EIP and EBP account for the other 8 bytes). This is
consistent with the fact that the i386 architecture caches data in
aligned 16-byte blocks (sometimes referred to as "paragraphs").

However, it treats sizes of 1, 2, 4 and 8 bytes as special cases,
allocating 4 bytes for the first three cases and 8 bytes for the last. 
Again, this very much looks like something which would be done for
optimisation reasons.

If you need to know the specific reasons, you would probably have to
ask the people who actually wrote the code. And even they might not
actually know why. Optimisation tends to involve dealing with opposing
pressures. Any particular technique will speed up some programs but
slow down others; the optimal approach can often only be determined by
trial and error. If a certain combination tends to make code run
slightly faster, it may be adopted without anyone actually analysing
it in detail.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-03-24 21:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-24 16:22 Value to subtract from ESP register? zavandi
2004-03-24 17:25 ` Chris Nanakos
2004-03-24 19:39   ` zavandi
2004-03-24 21:37     ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).