* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
@ 2014-04-22 21:29 ` gidici61 at gmail dot com
2014-04-22 21:31 ` pinskia at gcc dot gnu.org
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-22 21:29 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #2 from gidici61 at gmail dot com ---
Register rsp is correctly aligned before "call g1"; let's assume rsp=0xB0.
"call g1" pushes rip (8 bytes) on the stack. Now rsp=0xA8.
Then "pushq %rbp" subtracts 8. Now rsp=0xA0.
"subq $8, %rsp" subtracts 8. Now rsp=0x98
So before calling g2() rsp is not properly aligned.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
2014-04-22 21:29 ` [Bug target/60926] " gidici61 at gmail dot com
@ 2014-04-22 21:31 ` pinskia at gcc dot gnu.org
2014-04-22 21:42 ` gidici61 at gmail dot com
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-04-22 21:31 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to gidici61 from comment #2)
> Register rsp is correctly aligned before "call g1"; let's assume rsp=0xB0.
> "call g1" pushes rip (8 bytes) on the stack. Now rsp=0xA8.
> Then "pushq %rbp" subtracts 8. Now rsp=0xA0.
> "subq $8, %rsp" subtracts 8. Now rsp=0x98
> So before calling g2() rsp is not properly aligned.
call also pushes onto the stack.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
2014-04-22 21:29 ` [Bug target/60926] " gidici61 at gmail dot com
2014-04-22 21:31 ` pinskia at gcc dot gnu.org
@ 2014-04-22 21:42 ` gidici61 at gmail dot com
2014-04-22 21:53 ` gidici61 at gmail dot com
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-22 21:42 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #4 from gidici61 at gmail dot com ---
(In reply to Andrew Pinski from comment #3)
> (In reply to gidici61 from comment #2)
> > Register rsp is correctly aligned before "call g1"; let's assume rsp=0xB0.
> > "call g1" pushes rip (8 bytes) on the stack. Now rsp=0xA8.
> > Then "pushq %rbp" subtracts 8. Now rsp=0xA0.
> > "subq $8, %rsp" subtracts 8. Now rsp=0x98
> > So before calling g2() rsp is not properly aligned.
>
> call also pushes onto the stack.
If you add "putchar(p1)" in g2(), then rsp become 16-byte aligned before
calling g2():
int g2(int p1) { putchar(p1); return p1; }
int g1(int p1) { return g2(p1); }
int main() { return g1(65); }
gives the following assembler code:
g1:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp ; OK
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %edi
call g2
leave
ret
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (2 preceding siblings ...)
2014-04-22 21:42 ` gidici61 at gmail dot com
@ 2014-04-22 21:53 ` gidici61 at gmail dot com
2014-04-25 13:45 ` gidici61 at gmail dot com
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-22 21:53 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #5 from gidici61 at gmail dot com ---
Please see also:
http://stackoverflow.com/questions/22941372/gcc-4-7-2-in-debian-wheezy-doesnt-always-properly-align-stack-pointer-is-this/23055211#23055211
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (3 preceding siblings ...)
2014-04-22 21:53 ` gidici61 at gmail dot com
@ 2014-04-25 13:45 ` gidici61 at gmail dot com
2014-04-25 15:46 ` hjl.tools at gmail dot com
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-25 13:45 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #6 from gidici61 at gmail dot com ---
I don't understand why the addition of "putchar(p1)" in g2() C code changes
"subq $8, %rsp" to "subq $16 %rsp", while all the other assembler code remain
unchanged.
So, if the second ("subq $16 %rsp") makes rsp 16 byte aligned before "call g2",
then the first ("subq $8, %rsp") must be wrong and vice-versa.
Can you help me to understand what I'm missing? Thank you in advance.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (4 preceding siblings ...)
2014-04-25 13:45 ` gidici61 at gmail dot com
@ 2014-04-25 15:46 ` hjl.tools at gmail dot com
2014-04-25 16:26 ` gidici61 at gmail dot com
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: hjl.tools at gmail dot com @ 2014-04-25 15:46 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #7 from H.J. Lu <hjl.tools at gmail dot com> ---
Since GCC finds out that g2 only needs 8 byte stack alignment,
it only needs to align stack to 8 byte when calling g2.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (5 preceding siblings ...)
2014-04-25 15:46 ` hjl.tools at gmail dot com
@ 2014-04-25 16:26 ` gidici61 at gmail dot com
2014-04-25 16:30 ` gidici61 at gmail dot com
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-25 16:26 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #8 from gidici61 at gmail dot com ---
(In reply to H.J. Lu from comment #7)
> Since GCC finds out that g2 only needs 8 byte stack alignment,
> it only needs to align stack to 8 byte when calling g2.
Thank you. So this should be an exception? System V ABI x86, par. 3.2.2, page
16, says: "The end of the input argument area shall be aligned on a 16 (32, if
is
passed on stack) byte boundary. In other words, the value (%rsp + 8) is always
a multiple of 16 (32) when control is transferred to the function entry point".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (6 preceding siblings ...)
2014-04-25 16:26 ` gidici61 at gmail dot com
@ 2014-04-25 16:30 ` gidici61 at gmail dot com
2014-04-25 16:40 ` gidici61 at gmail dot com
2014-04-25 17:45 ` hjl.tools at gmail dot com
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-25 16:30 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #9 from gidici61 at gmail dot com ---
I apologize. I forgot to specify that I used a 64 bit version of OS.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (7 preceding siblings ...)
2014-04-25 16:30 ` gidici61 at gmail dot com
@ 2014-04-25 16:40 ` gidici61 at gmail dot com
2014-04-25 17:45 ` hjl.tools at gmail dot com
9 siblings, 0 replies; 11+ messages in thread
From: gidici61 at gmail dot com @ 2014-04-25 16:40 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #10 from gidici61 at gmail dot com ---
(In reply to gidici61 from comment #9)
> I apologize. I forgot to specify that I used a 64 bit version of OS.
I should have written "System V ABi x86-64".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug target/60926] Compiler doesn't properly align stack pointer
2014-04-22 21:08 [Bug c/60926] New: Compiler doesn't properly align stack pointer gidici61 at gmail dot com
` (8 preceding siblings ...)
2014-04-25 16:40 ` gidici61 at gmail dot com
@ 2014-04-25 17:45 ` hjl.tools at gmail dot com
9 siblings, 0 replies; 11+ messages in thread
From: hjl.tools at gmail dot com @ 2014-04-25 17:45 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60926
--- Comment #11 from H.J. Lu <hjl.tools at gmail dot com> ---
(In reply to gidici61 from comment #8)
> (In reply to H.J. Lu from comment #7)
> > Since GCC finds out that g2 only needs 8 byte stack alignment,
> > it only needs to align stack to 8 byte when calling g2.
>
> Thank you. So this should be an exception? System V ABI x86, par. 3.2.2,
It is an optimization.
^ permalink raw reply [flat|nested] 11+ messages in thread