From: Peter Barada <peter@the-baradas.com>
To: sam124@operamail.com
Cc: nathan@codesourcery.com, gcc@gcc.gnu.org
Subject: Re: Unomitted frame pointers
Date: Sun, 12 Dec 2004 00:09:00 -0000 [thread overview]
Message-ID: <20041212000941.92E8F9842C@baradas.org> (raw)
In-Reply-To: <20041211162555.C0BE723CE8@ws5-3.us4.outblaze.com> (sam124@operamail.com)
>Do you know about macro expansion? CPP would make it seperate strings. So there would be (simplified);
>
>.LC0:
>.string "Hello World!\n"
>.LC1:
>.string "Hello World!\n"
>main:
>; push LC0 into strlen
>push #.LC0, (%eax)
>call strlen
>; push LC1 into write
>push #.LC1, (%ebx)
>...
>
>It automatically happens, and I don't know of any way to avoid cpp from macro-expanding it to that. Though inlining would reduce it to the C code
>
>int main(void)
>{
> char str1 = "Hello World!\n";
> char str2 = "Hello World!\n";
> int str_len = 0;
> while (*str1++ != "\0")
> str_len++;
> write(2, str2, str_len);
>}
>
>embedding strlen inside main. Or even more optimization
>
>int main(void)
>{
> write(2, "Hello World!\n", 13);
>}
Once I reduced your testcase to:
#include <string.h>
int main(void)
{
char *str1 = "Hello World!\n";
write(2, str1, strlen(str1));
}
And compiled it off the current mainline for --target=m68k-elf I get:
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Hello World!\n"
.text
.align 2
.globl main
.type main, @function
main:
link.w %fp,#0
pea 13.w ; <<<< strlen(str1)
pea .LC0
pea 2.w
jbsr write
lea (12,%sp),%sp
unlk %fp
rts
If you notice, the strlen is completely removed and replaced with 'pea
13.w' which does what you hope it does.
Even if I changed the code to:
#include <string.h>
#define STR "Hello World!\n"
int main(void)
{
char *str1 = STR;
write(2, str1, strlen(STR));
}
GCC produces the same code. Even gcc-2.96 on my Linux x86 box
knows to convert the strlen call into a constant.
Use the standard named functions since GCC is smart enough to figure
out(with optimization enabled) that 'strlen(str1)' is 13 since between
the declaration of str1 and the call to strlen, str1 did not change
and GCC knows what strlen of a constant string is, or a pointer to a
string constant that it see doesn't change between its assignment and
the invocation.
Hopefully this helps...
--
Peter Barada
peter@the-baradas.com
next prev parent reply other threads:[~2004-12-12 0:09 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-11 16:26 Sam Lauber
2004-12-11 16:42 ` Nathan Sidwell
2004-12-12 0:09 ` Peter Barada [this message]
-- strict thread matches above, loose matches on Subject: below --
2004-12-11 16:29 Sam Lauber
2004-12-12 1:33 ` Ian Lance Taylor
2004-12-11 16:15 Sam Lauber
2004-12-11 17:00 ` Eric Botcazou
2004-12-11 18:10 ` Robert Dewar
2004-12-13 6:16 ` Ranjit Mathew
2004-12-13 14:22 ` Robert Dewar
2004-12-13 23:36 ` Ben Elliston
2004-12-11 2:44 Sam Lauber
2004-12-11 15:40 ` Nathan Sidwell
2004-12-11 15:45 ` Andreas Schwab
2004-12-11 2:37 Sam Lauber
2004-12-10 15:44 Thomas R. Truscott
2004-12-10 16:05 ` jlh
2004-12-10 16:25 ` Dave Korn
2004-12-10 16:30 ` Nathan Sidwell
2004-12-10 16:41 ` Dave Korn
2004-12-10 17:54 ` Nathan Sidwell
2004-12-10 16:33 ` Andreas Schwab
2004-12-10 16:40 ` Dave Korn
2004-12-10 16:55 ` Thomas R. Truscott
2004-12-10 17:26 ` Paul Brook
2004-12-11 11:53 ` Kai Henningsen
2004-12-10 5:26 Sam Lauber
2004-12-10 7:22 ` Eric Botcazou
2004-12-10 9:08 ` Richard Guenther
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20041212000941.92E8F9842C@baradas.org \
--to=peter@the-baradas.com \
--cc=gcc@gcc.gnu.org \
--cc=nathan@codesourcery.com \
--cc=sam124@operamail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).