Re: [cc65] statics in zero page

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2002-05-21 14:59:31


Hi!

On Tue, May 21, 2002 at 08:55:06AM -0400, Keates, Mark wrote:
> Oddly enough, if you make it "const char msg[]" then
> it does go into the RODATA segment. The was a thread on
> the difference between the two in the uClinux mail-list
> entitled 'Silly C Question'.

This invalidates the answer I had already written:-) I'll add it here if
anyone else is interested in an explanation why you code did not do as you
expected.

----------------------------------------------------------------------------

On Tue, May 21, 2002 at 08:00:54AM -0400, Keates, Mark wrote:
> After a little experiment I think there maybe a bug here
> but that'll need to be confirmed as I'm not sure how
> this should work. I tried my test.c file:

It's not a bug, it's a feature:-)

The declaration

        char* Msg = "Hello world\n";

declares a pointer to a string literal. The string literal goes into the
literal pool, which is placed into the data segment if string literals are
supposed to be writeable (which violates the C standard, so it's not the
default) and is placed into the read only data segment by default (string
literals are not writeable). The pointer is writeable (because it is not
declared as const), so it goes into the data segment.

Using

        #pragma rodataseg ("zp")
        char* Msg = "Hello world\n";
        #pragma rodataseg ("RODATA")

has two problems:

  1. Because the pointer goes into the data segment, changing the name of the
     rodata segment has no effect.

  2. If you would declare the pointer to be const, the change of the name of
     the rodata segment would only have an effect on the pointer, not on the
     string.

The actual problem is that you are using a pointer for no real purpose. Apart
from costing two additional bytes, it generates worse code. If you use

        const char Msg[] = "Hello world\n";

instead, you are declaring a character array, which saves two bytes for the
pointer, allows the compiler to generate better code in many situations, and
in addition to all that,

        #pragma rodataseg ("zp")
        const char Msg[] = "Hello world\n";
        #pragma rodataseg ("RODATA")

will do what you want (change the segment the string data is placed into).

----------------------------------------------------------------------------

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-05-21 14:59:31 CEST