This is the mail archive of the gcc-help@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: GCC labels as values extension and scope


On Tue, 11 Apr 2017, sztfg@yandex.ru wrote:
> #include <stdio.h>
> 
> void *labels[3];
> 
> void test(void *ptr)
> {
>     if(ptr == NULL)
>     {
>       labels[0] = &&l1;
>       labels[1] = &&l2;
>       labels[2] = &&l3;
>       return;
>     }
>     goto *ptr;
> l1:
>     printf("test1\n");
>     return;
> l2:
>     printf("test2\n");
>     return;
> l3:
>     printf("test3\n");
>     return;
> }
> 
> int main(void)
> {
>    test(NULL);
>    test(labels[0]);
>    test(labels[1]);
>    test(labels[2]);
> }
> 
> It is possible to get label pointer without adding this if(ptr == NULL) {
> blah-blah } stuff and global array?

No. Also note that this code is _not_ guaranteed to work as expected in recent
releases of GCC, because, in short, the compiler treats labels similarly to
automatic variables, meaning that their address is considered valid only
until the function returns.  Optimizations that clone functions need to
duplicate labels, and that will break code that smuggles label addresses
to outer frames.  See PR 80053 comment 2 for a sample that gets "miscompiled":
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80053#c2

HTH
Alexander


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