public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* What is the address of this line?
@ 2006-06-19  5:38 John Carter
  2006-06-20  0:15 ` Brian Dessent
  0 siblings, 1 reply; 4+ messages in thread
From: John Carter @ 2006-06-19  5:38 UTC (permalink / raw)
  To: gcc-help

So we using gcc on an embedded platform. ie. Very constrained flash & ram.

We also using asserts.

Storing __LINE__ and __FILE__ is taking too much flash.

Question: I would like to create an assert macro ....

#define Assert( expression) do {\
   if(!(expression)) { \
     assert_occurred_at_program_counter = THE_ADDRESS_OF_THIS_LINE(); \
   }
} while(0)


So how do I write that magical function / macro
THE_ADDRESS_OF_THIS_LINE(); That returns the address / PC at that line?
Preferable in a CPU neutral fashion, otherwise for a Sparc CPU.



John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

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

* Re: What is the address of this line?
  2006-06-19  5:38 What is the address of this line? John Carter
@ 2006-06-20  0:15 ` Brian Dessent
  2006-06-20  9:01   ` Andrew Haley
  0 siblings, 1 reply; 4+ messages in thread
From: Brian Dessent @ 2006-06-20  0:15 UTC (permalink / raw)
  To: gcc-help

John Carter wrote:

> So we using gcc on an embedded platform. ie. Very constrained flash & ram.
> 
> We also using asserts.
> 
> Storing __LINE__ and __FILE__ is taking too much flash.
> 
> Question: I would like to create an assert macro ....
> 
> #define Assert( expression) do {\
>    if(!(expression)) { \
>      assert_occurred_at_program_counter = THE_ADDRESS_OF_THIS_LINE(); \
>    }
> } while(0)
> 
> So how do I write that magical function / macro
> THE_ADDRESS_OF_THIS_LINE(); That returns the address / PC at that line?
> Preferable in a CPU neutral fashion, otherwise for a Sparc CPU.

How about something like: (see also section 5.2 of the manual)

#define Assert(expression) ({ \
    __label__ here; \
    if (!(expression)) { \
        here: assert_occurred_at_program_counter = &&here; \
    } \
})

Brian

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

* Re: What is the address of this line?
  2006-06-20  0:15 ` Brian Dessent
@ 2006-06-20  9:01   ` Andrew Haley
  2006-06-21  7:26     ` Ingo Krabbe
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Haley @ 2006-06-20  9:01 UTC (permalink / raw)
  To: Brian Dessent; +Cc: gcc-help

Brian Dessent writes:
 > John Carter wrote:
 > 
 > > So we using gcc on an embedded platform. ie. Very constrained flash & ram.
 > > 
 > > We also using asserts.
 > > 
 > > Storing __LINE__ and __FILE__ is taking too much flash.
 > > 
 > > Question: I would like to create an assert macro ....
 > > 
 > > #define Assert( expression) do {\
 > >    if(!(expression)) { \
 > >      assert_occurred_at_program_counter = THE_ADDRESS_OF_THIS_LINE(); \
 > >    }
 > > } while(0)
 > > 
 > > So how do I write that magical function / macro
 > > THE_ADDRESS_OF_THIS_LINE(); That returns the address / PC at that line?
 > > Preferable in a CPU neutral fashion, otherwise for a Sparc CPU.
 > 
 > How about something like: (see also section 5.2 of the manual)
 > 
 > #define Assert(expression) ({ \
 >     __label__ here; \
 >     if (!(expression)) { \
 >         here: assert_occurred_at_program_counter = &&here; \
 >     } \
 > })

I also thought of

void *foo ()
{
  return __builtin_return_address(0);
}

Andrew.

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

* Re: What is the address of this line?
  2006-06-20  9:01   ` Andrew Haley
@ 2006-06-21  7:26     ` Ingo Krabbe
  0 siblings, 0 replies; 4+ messages in thread
From: Ingo Krabbe @ 2006-06-21  7:26 UTC (permalink / raw)
  To: gcc-help; +Cc: Andrew Haley, Brian Dessent

Am Dienstag, 20. Juni 2006 11:01 schrieb Andrew Haley:
> Brian Dessent writes:
>  > John Carter wrote:
>  > > So we using gcc on an embedded platform. ie. Very constrained flash &
>  > > ram.
>  > >
>  > > We also using asserts.
>  > >
>  > > Storing __LINE__ and __FILE__ is taking too much flash.
>  > >
>  > > Question: I would like to create an assert macro ....
>  > >
>  > > #define Assert( expression) do {\
>  > >    if(!(expression)) { \
>  > >      assert_occurred_at_program_counter = THE_ADDRESS_OF_THIS_LINE();
>  > > \ }
>  > > } while(0)
>  > >
>  > > So how do I write that magical function / macro
>  > > THE_ADDRESS_OF_THIS_LINE(); That returns the address / PC at that
>  > > line? Preferable in a CPU neutral fashion, otherwise for a Sparc CPU.
>  >
>  > How about something like: (see also section 5.2 of the manual)
>  >
>  > #define Assert(expression) ({ \
>  >     __label__ here; \
>  >     if (!(expression)) { \
>  >         here: assert_occurred_at_program_counter = &&here; \
>  >     } \
>  > })
>
> I also thought of
>
> void *foo ()
> {
>   return __builtin_return_address(0);
> }
>
> Andrew.

Anyway, do you really think you need this ? On an embedded platform you won't 
want the program to step out at assertions, do you ?  In the normal case, you 
only have the assertions in while you develop, on your development platform.  
Hopefully you are able to run in a simulator on your development plaftorm.

Once you compile the product for the embedded machine, you will use -DNDEBUG 
to disable the asserts.  This is the meaning and the idea behind the assert 
macro.

__LINE__ and __FILE__ aren't used to get a runtime PC of the code, but are 
debugging informations to throw out sourcecode lines at illegal behaviour.  
It seems that |__builtin_return_adress(0)| is a valid way to read PC, though.

bye ingo

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

end of thread, other threads:[~2006-06-21  7:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-19  5:38 What is the address of this line? John Carter
2006-06-20  0:15 ` Brian Dessent
2006-06-20  9:01   ` Andrew Haley
2006-06-21  7:26     ` Ingo Krabbe

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