* GCC Internals: built-in functions?
@ 2011-01-30 20:50 Amittai Aviram
2011-01-30 21:01 ` Jonathan Wakely
0 siblings, 1 reply; 5+ messages in thread
From: Amittai Aviram @ 2011-01-30 20:50 UTC (permalink / raw)
To: gcc-help
On this GCC Internals page
http://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Architecture_3_4
I found the following in the "GCC Initialization" section:
"GCC built-in functions are the functions that are evaluated at compile time. For example, if the size argument of a strcpy() function is a constant then GCC replaces the function call with the required number of assignments."
I was curious about this, so I tried compiling to assembly (-S) a very simple program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char s1[0x10];
char * s0 = "HELLO";
strcpy(s1, s0);
printf("%s %s\n", s0, s1);
return EXIT_SUCCESS;
}
But the resulting assembly code simply calls strcpy with the two arguments, just as I would have expected had I not read the above sentence:
movq $.LC0, -40(%rbp)
movq -40(%rbp), %rdx
leaq -32(%rbp), %rax
movq %rdx, %rsi
movq %rax, %rdi
call strcpy
(Here, .LC0 labels the string "HELLO".)
So what does that sentence actually mean and what am I missing? Thanks!
Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram@yale.edu
http://www.amittai.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: GCC Internals: built-in functions?
2011-01-30 20:50 GCC Internals: built-in functions? Amittai Aviram
@ 2011-01-30 21:01 ` Jonathan Wakely
2011-01-30 21:07 ` Amittai Aviram
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2011-01-30 21:01 UTC (permalink / raw)
To: Amittai Aviram; +Cc: gcc-help
On 30 January 2011 20:45, Amittai Aviram wrote:
> On this GCC Internals page
>
> http://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Architecture_3_4
>
> I found the following in the "GCC Initialization" section:
>
> "GCC built-in functions are the functions that are evaluated at compile time. For example, if the size argument of a strcpy() function is a constant then GCC replaces the function call with the required number of assignments."
>
> I was curious about this, so I tried compiling to assembly (-S) a very simple program:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(void) {
>
> char s1[0x10];
> char * s0 = "HELLO";
> strcpy(s1, s0);
> printf("%s %s\n", s0, s1);
> return EXIT_SUCCESS;
> }
>
> But the resulting assembly code simply calls strcpy with the two arguments, just as I would have expected had I not read the above sentence:
>
> movq $.LC0, -40(%rbp)
> movq -40(%rbp), %rdx
> leaq -32(%rbp), %rax
> movq %rdx, %rsi
> movq %rax, %rdi
> call strcpy
>
> (Here, .LC0 labels the string "HELLO".)
>
> So what does that sentence actually mean and what am I missing? Thanks!
strcpy has no 'size' parameter, I assume it's meant to say strncpy
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: GCC Internals: built-in functions?
2011-01-30 21:01 ` Jonathan Wakely
@ 2011-01-30 21:07 ` Amittai Aviram
2011-01-30 21:28 ` Jonathan Wakely
0 siblings, 1 reply; 5+ messages in thread
From: Amittai Aviram @ 2011-01-30 21:07 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: gcc-help
On Jan 30, 2011, at 3:57 PM, Jonathan Wakely wrote:
> On 30 January 2011 20:45, Amittai Aviram wrote:
>> On this GCC Internals page
>>
>> http://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Architecture_3_4
>>
>> I found the following in the "GCC Initialization" section:
>>
>> "GCC built-in functions are the functions that are evaluated at compile time. For example, if the size argument of a strcpy() function is a constant then GCC replaces the function call with the required number of assignments."
>>
>> I was curious about this, so I tried compiling to assembly (-S) a very simple program:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> int main(void) {
>>
>> char s1[0x10];
>> char * s0 = "HELLO";
>> strcpy(s1, s0);
>> printf("%s %s\n", s0, s1);
>> return EXIT_SUCCESS;
>> }
>>
>> But the resulting assembly code simply calls strcpy with the two arguments, just as I would have expected had I not read the above sentence:
>>
>> movq $.LC0, -40(%rbp)
>> movq -40(%rbp), %rdx
>> leaq -32(%rbp), %rax
>> movq %rdx, %rsi
>> movq %rax, %rdi
>> call strcpy
>>
>> (Here, .LC0 labels the string "HELLO".)
>>
>> So what does that sentence actually mean and what am I missing? Thanks!
>
> strcpy has no 'size' parameter, I assume it's meant to say strncpy
I tried it with strncpy and with __builtin_strncpy, with exactly the same results, i.e., the assembly code still calls strncpy with three arguments.
Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram@yale.edu
http://www.amittai.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: GCC Internals: built-in functions?
2011-01-30 21:07 ` Amittai Aviram
@ 2011-01-30 21:28 ` Jonathan Wakely
2011-01-31 4:53 ` Amittai Aviram
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2011-01-30 21:28 UTC (permalink / raw)
To: Amittai Aviram; +Cc: gcc-help
On 30 January 2011 21:02, Amittai Aviram wrote:
>
> I tried it with strncpy and with __builtin_strncpy, with exactly the same results, i.e., the assembly code still calls strncpy with three arguments.
Did you enable optimization?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: GCC Internals: built-in functions?
2011-01-30 21:28 ` Jonathan Wakely
@ 2011-01-31 4:53 ` Amittai Aviram
0 siblings, 0 replies; 5+ messages in thread
From: Amittai Aviram @ 2011-01-31 4:53 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: gcc-help
On Jan 30, 2011, at 4:07 PM, Jonathan Wakely wrote:
> On 30 January 2011 21:02, Amittai Aviram wrote:
>>
>> I tried it with strncpy and with __builtin_strncpy, with exactly the same results, i.e., the assembly code still calls strncpy with three arguments.
>
> Did you enable optimization?
I hadn't and that did it--even with just "-O1." Thank you! Very interesting!—
movl $1280066888, (%rsp)
movw $79, 4(%rsp)
The first integer in hex is 4C 4C 45 48 ( == 'L','L','E','H')
and the second (79) is 4F ( == 'O')
So, of course, it works out when assigned in little-endian order. The second assignment also puts NULL in the right place as the higher-order bits of the integer 79.
Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram@yale.edu
http://www.amittai.com
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-01-30 21:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-30 20:50 GCC Internals: built-in functions? Amittai Aviram
2011-01-30 21:01 ` Jonathan Wakely
2011-01-30 21:07 ` Amittai Aviram
2011-01-30 21:28 ` Jonathan Wakely
2011-01-31 4:53 ` Amittai Aviram
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 read-only IMAP folder(s) and NNTP newsgroup(s).