public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* [Fwd: Re: initializing ints with mem refs (ANSI C??)]
@ 2000-03-05  9:29 Ralf Guetlein
  2000-04-01  0:00 ` Ralf Guetlein
  0 siblings, 1 reply; 2+ messages in thread
From: Ralf Guetlein @ 2000-03-05  9:29 UTC (permalink / raw)
  To: gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2180 bytes --]

Moogla,
thank you for your reply

> 
> Ralf Gütlein wrote:
> 
> > const int object = 0;
> > int a[2] = {1, (const int)&object;};

Moogla wrote:
> Is there any problem with:
> const int object = 0;
> int a[2];
> a[0] = 1;
> a[1] = (const int) &object;

I should have written:

enum {item1, item2, item3, item4};

const int menu1[] = {item1, item2, item3};
const int menu2[] = {item4, (const int)menu1};

What I intended to do is generating a linked list in ROM
to implement a hierachical menu structure. In gcc this
is no problem, as I stated earlier.

>
> > What does the ANSI standard demand here?
> 
> ANSI only requires it to minimally be a constant expression, one that
> can be evaluated at compile time. Of course, we know the address of
> object at compile time, but strictly, it's not a constant. 

The difference between 'item1' and '(const int)menu1' is that the latter
is evaluated at *link* time. So you are right in that it is not a
*compile* time constant. This is no problem for gcc.

But IMO this should be generally no problem because

typedef void* PVOID;

enum {item1, item2, item3, item4};

const PVOID menu1[] = {(PVOID)item1, (PVOID)item2, (PVOID)item3};
const PVOID menu2[] = {(PVOID)item4, (PVOID)menu1};

works with both compilers, although '(PVOID)menu1' again can be
calculated only at link time.
(The assembler listing issued by gcc is exactly the same for both
of the above contructs, and the IAR assembler listing for the 
latter one is functionally identical to those made by gcc.)


> a subexpression elimination pass which rejects address references, and
> this pass is used to verify aggregate initializers. Also, IAR may feel
> it wants to move addresses of variables around in the data segment if
> it's feeling particularly optimizationy or whatever. GCC, oth, only
> resolves the initialization until it knows what the final value will be.
> I don't think you can count on that in future releases of EGCS, though.
> 

I guess the compiler behaviour in these cases depends in no way on
any kind of optimization. It's merely a restriction implemented by
the compiler writers (with or without intent).

> moogla

Thanks again. Regards,

Ralf

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Fwd: Re: initializing ints with mem refs (ANSI C??)]
  2000-03-05  9:29 [Fwd: Re: initializing ints with mem refs (ANSI C??)] Ralf Guetlein
@ 2000-04-01  0:00 ` Ralf Guetlein
  0 siblings, 0 replies; 2+ messages in thread
From: Ralf Guetlein @ 2000-04-01  0:00 UTC (permalink / raw)
  To: gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2180 bytes --]

Moogla,
thank you for your reply

> 
> Ralf Gütlein wrote:
> 
> > const int object = 0;
> > int a[2] = {1, (const int)&object;};

Moogla wrote:
> Is there any problem with:
> const int object = 0;
> int a[2];
> a[0] = 1;
> a[1] = (const int) &object;

I should have written:

enum {item1, item2, item3, item4};

const int menu1[] = {item1, item2, item3};
const int menu2[] = {item4, (const int)menu1};

What I intended to do is generating a linked list in ROM
to implement a hierachical menu structure. In gcc this
is no problem, as I stated earlier.

>
> > What does the ANSI standard demand here?
> 
> ANSI only requires it to minimally be a constant expression, one that
> can be evaluated at compile time. Of course, we know the address of
> object at compile time, but strictly, it's not a constant. 

The difference between 'item1' and '(const int)menu1' is that the latter
is evaluated at *link* time. So you are right in that it is not a
*compile* time constant. This is no problem for gcc.

But IMO this should be generally no problem because

typedef void* PVOID;

enum {item1, item2, item3, item4};

const PVOID menu1[] = {(PVOID)item1, (PVOID)item2, (PVOID)item3};
const PVOID menu2[] = {(PVOID)item4, (PVOID)menu1};

works with both compilers, although '(PVOID)menu1' again can be
calculated only at link time.
(The assembler listing issued by gcc is exactly the same for both
of the above contructs, and the IAR assembler listing for the 
latter one is functionally identical to those made by gcc.)


> a subexpression elimination pass which rejects address references, and
> this pass is used to verify aggregate initializers. Also, IAR may feel
> it wants to move addresses of variables around in the data segment if
> it's feeling particularly optimizationy or whatever. GCC, oth, only
> resolves the initialization until it knows what the final value will be.
> I don't think you can count on that in future releases of EGCS, though.
> 

I guess the compiler behaviour in these cases depends in no way on
any kind of optimization. It's merely a restriction implemented by
the compiler writers (with or without intent).

> moogla

Thanks again. Regards,

Ralf

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-04-01  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-05  9:29 [Fwd: Re: initializing ints with mem refs (ANSI C??)] Ralf Guetlein
2000-04-01  0:00 ` Ralf Guetlein

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).