This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Removing -frwitable-strings


I wrote:
> > > It would seem that the front end would need to generate a static
> > > char[] array for each literal string, and use its address as the value
> > > of the literal string expression.  If that were done, the back end could
> > > eliminate its support for -fwritable-strings.

Mark Mitchell wrote:
> > Yes.  
> > 
> > But, even that will not work because there are places in the front ends
> > that expect string constants to be STRING_CSTs.
> > 
> > It's possible that the conversion you suggest could be done at tree->rtl
> > conversion time.  That is the approach I would try if I had to implement
> > it.
 
On Thu, Dec 18, 2003 at 10:49:18PM +0000, Jamie Lokier wrote:
> I'm not sure that would work.  STRING_CST seems to suggest it's a
> _constant_ and some tree-level or tree generation optimisers might
> think they can constant-fold things like dereferencing characters in
> the string or strlen().  Or at least, they ought to be able to assume
> that without checking the -fwritable-strings flag.

The answer, I think, is for the compiler never to assume that there is
such a thing as a string constant.  That is, if the user writes:

#include <stdio.h>
int main() {
	printf("Hello, world\n");
}

and compiles with -fwritable-strings, the front end emits code exactly
as if the user wrote

#include <stdio.h>
int main() {
	static char __tmp[] = "Hello, world\n";
	printf(__tmp);
}

Since -fwritable-strings says that all strings are modifiable, there
are no such things as string constants any more, only initializers for
writable character arrays.  The compiler can assume if it wishes that
the string in the transformed program that is then used to initialize
the static character array is constant; this does not matter.


 
> Those optimisation aren't valid when strings are writable, so it makes
> sense to represent writable strings at the tree level and at the front
> end, for _expressions_ only, as char[] arrays instead.
> 
> (Strings still need to be strings for concatenation and string
> initialises in C; otherwise, static char[] arrays are a fine
> representation).
> 
> Is there anything wrong with this C source level transformation when
> "string" is an expression, as opposed to structure initialiser?
>    
>        "string"
> 
> becomes
> 
>        /* Magically outside function scope. */
>        static char x[] = "string";
>        /* Back to your regular function scope. */
>        x
> 
> 
> ({ static char x[] = "string"; x; }) doesn't work because of sizeof(),
> and ((char[]) { "string" }) doesn't work because it's not static.  But
> that's just C language difficulties; the C frontend should have no
> difficulty creating a static anonymous array of the appropriate kind.
> 
> -- Jamie

-- 
Q. What's more of a headache than a bug in a compiler.
A. Bugs in six compilers.  -- Mark Johnson


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]