public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* "lang_statement_union_type and its usage?"
@ 2001-05-31 11:57 Xinan Tang
  2001-05-31 12:11 ` Ian Lance Taylor
  0 siblings, 1 reply; 5+ messages in thread
From: Xinan Tang @ 2001-05-31 11:57 UTC (permalink / raw)
  To: binutils

Hi

  In ld/ldlang.h, lang_statement_union_type is defined as:

typedef union lang_statement_union
{
  lang_statement_header_type header;
  union lang_statement_union *next;
  ... /* Other stuffs */
} lang_statement_union_type;


  However, I saw in many places in `ldlang.c', the list is traversed as
follows:

   for (; s != (lang_statement_union_type *) NULL; s = s->next)
    {
      switch (s->header.type)
    ...
    }

  The question is that the 'next' and 'header' fields are exclusive, how
could they be used at the same time. Could some one inlight me why
this usage is SAFE?

  My goal is to traverse the statement list at the linking time. I have
difficulty to understand why the list is searched in this way.

Thanks



-- 
Dr. Xinan Tang                    Member of Technical Staff
EMail: xinant@cognigine.com  	  Cognigine Corp.
Voice: 510.743.4930               6120 Stevenson Boulevard
Fax:   510.743.4910               Fremont, CA  94538

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

* Re: "lang_statement_union_type and its usage?"
  2001-05-31 11:57 "lang_statement_union_type and its usage?" Xinan Tang
@ 2001-05-31 12:11 ` Ian Lance Taylor
  2001-05-31 13:47   ` Xinan Tang
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2001-05-31 12:11 UTC (permalink / raw)
  To: Xinan Tang; +Cc: binutils

xinant@cognigine.com (Xinan Tang) writes:

>   In ld/ldlang.h, lang_statement_union_type is defined as:
> 
> typedef union lang_statement_union
> {
>   lang_statement_header_type header;
>   union lang_statement_union *next;
>   ... /* Other stuffs */
> } lang_statement_union_type;
> 
> 
>   However, I saw in many places in `ldlang.c', the list is traversed as
> follows:
> 
>    for (; s != (lang_statement_union_type *) NULL; s = s->next)
>     {
>       switch (s->header.type)
>     ...
>     }
> 
>   The question is that the 'next' and 'header' fields are exclusive, how
> could they be used at the same time. Could some one inlight me why
> this usage is SAFE?
> 
>   My goal is to traverse the statement list at the linking time. I have
> difficulty to understand why the list is searched in this way.

All elements of lang_statement_union start with
  lang_statement_header_type header;
The first field in lang_statement_header_type is
  union lang_statement_union *next;

Therefore, in all cases, the first field in any instance of
lang_statement_union will be the next pointer.

This is a pretty common hack when implementing derived classes in C.

Ian

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

* Re: "lang_statement_union_type and its usage?"
  2001-05-31 12:11 ` Ian Lance Taylor
@ 2001-05-31 13:47   ` Xinan Tang
  2001-05-31 13:56     ` Geoff Keating
  2001-05-31 14:10     ` Ian Lance Taylor
  0 siblings, 2 replies; 5+ messages in thread
From: Xinan Tang @ 2001-05-31 13:47 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: binutils

Hi Ian,

Ian Lance Taylor wrote:

 
> 
> All elements of lang_statement_union start with
>   lang_statement_header_type header;
> The first field in lang_statement_header_type is
>   union lang_statement_union *next;
> 
> Therefore, in all cases, the first field in any instance of
> lang_statement_union will be the next pointer.
> 
> This is a pretty common hack when implementing derived classes in C.
> 
> Ian

   I see. However, this may not work if some optimizing compilers
would change the relative order of the `next' field. I.e., this scheme
is compiler-dependent.

Thanks

-- 
Dr. Xinan Tang                    Member of Technical Staff
EMail: xinant@cognigine.com  	  Cognigine Corp.
Voice: 510.743.4930               6120 Stevenson Boulevard
Fax:   510.743.4910               Fremont, CA  94538

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

* Re: "lang_statement_union_type and its usage?"
  2001-05-31 13:47   ` Xinan Tang
@ 2001-05-31 13:56     ` Geoff Keating
  2001-05-31 14:10     ` Ian Lance Taylor
  1 sibling, 0 replies; 5+ messages in thread
From: Geoff Keating @ 2001-05-31 13:56 UTC (permalink / raw)
  To: xinant; +Cc: ian, binutils

> Date: Thu, 31 May 2001 13:47:24 -0700
> From: xinant@cognigine.com (Xinan Tang)

> > 
> > All elements of lang_statement_union start with
> >   lang_statement_header_type header;
> > The first field in lang_statement_header_type is
> >   union lang_statement_union *next;
> > 
> > Therefore, in all cases, the first field in any instance of
> > lang_statement_union will be the next pointer.
> > 
> > This is a pretty common hack when implementing derived classes in C.
> > 
> > Ian
> 
>    I see. However, this may not work if some optimizing compilers
> would change the relative order of the `next' field. I.e., this scheme
> is compiler-dependent.

I believe that the C standard promises that this works.

-- 
- Geoffrey Keating <geoffk@geoffk.org>

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

* Re: "lang_statement_union_type and its usage?"
  2001-05-31 13:47   ` Xinan Tang
  2001-05-31 13:56     ` Geoff Keating
@ 2001-05-31 14:10     ` Ian Lance Taylor
  1 sibling, 0 replies; 5+ messages in thread
From: Ian Lance Taylor @ 2001-05-31 14:10 UTC (permalink / raw)
  To: Xinan Tang; +Cc: binutils

xinant@cognigine.com (Xinan Tang) writes:

> > All elements of lang_statement_union start with
> >   lang_statement_header_type header;
> > The first field in lang_statement_header_type is
> >   union lang_statement_union *next;
> > Therefore, in all cases, the first field in any instance of
> > lang_statement_union will be the next pointer.
> > This is a pretty common hack when implementing derived classes in C.
> > Ian
> 
>    I see. However, this may not work if some optimizing compilers
> would change the relative order of the `next' field. I.e., this scheme
> is compiler-dependent.

The ISO C standard forbids compilers from making the possible
optimization.  ISO C requires that structure members have addresses
that increase in the order in which they are declared.  ISO C also
says ``a pointer to a structure object, suitably converted, points to
its initial member'' and ``a pointer to a union object, suitably
converted, points to each of its members.''

This is required in order to make it possible for a C structure or
union to describe an area of memory used by hardware, a traditional
use of C in kernel driver implementations.

C is not a high level language.

Ian

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

end of thread, other threads:[~2001-05-31 14:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-31 11:57 "lang_statement_union_type and its usage?" Xinan Tang
2001-05-31 12:11 ` Ian Lance Taylor
2001-05-31 13:47   ` Xinan Tang
2001-05-31 13:56     ` Geoff Keating
2001-05-31 14:10     ` Ian Lance Taylor

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