This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: GCC labels as values extension and scope
- From: Alexander Monakov <amonakov at ispras dot ru>
- To: sztfg at yandex dot ru
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 11 Apr 2017 18:52:35 +0300 (MSK)
- Subject: Re: GCC labels as values extension and scope
- Authentication-results: sourceware.org; auth=none
- References: <1982681491922983@web12g.yandex.ru>
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