public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: Archie Cobbs <archie@dellroad.org>
To: nobody@gcc.gnu.org
Cc: gcc-prs@gcc.gnu.org,
Subject: Re: c/10231: Request a "reachable" attribute for labels.
Date: Wed, 26 Mar 2003 23:16:00 -0000	[thread overview]
Message-ID: <20030326230601.30298.qmail@sources.redhat.com> (raw)

The following reply was made to PR c/10231; it has been noted by GNATS.

From: Archie Cobbs <archie@dellroad.org>
To: Zack Weinberg <zack@codesourcery.com>
Cc: archie@dellroad.org, gcc-gnats@gcc.gnu.org
Subject: Re: c/10231: Request a "reachable" attribute for labels.
Date: Wed, 26 Mar 2003 14:47:41 -0800 (PST)

 Zack Weinberg wrote:
 > >      goto label3;
 > >   label2:
 > >       printf("don't eliminate me\n");
 > >   label3:
 > >       printf("ok I'm here now\n");
 > >
 > > When optimizing -O2 GCC will eliminate the code between
 > > label2 and label3. However I have an application where
 > > I need this code to remain, because I've made the address
 > > of label2 available (via "&&label2") and also have some
 > > asm() statements that are used to jump to label2 that
 > > GCC doesn't know about.
 > 
 > Go up a level and tell us what you're really trying to do.  The
 > odds are that you can do it without needing to play games with
 > asm statements jumping to labels.  That has never been supported,
 > as you will discover if you look at the manual.
 
 OK...
 
 This is for an application that implements a higher layer language
 by automatically generating C code. The higher layer language includes
 support for throwing and catching exceptions, very similar to how Java
 works (unfortunately, generating C++ instead of C is not an option).
 
 I could use setjmp/longjmp; however, because of the way the code
 is generated all pre-existing local variables used in the 'catch'
 block are volatile, so I don't need to save and restore registers,
 so setjmp/longjmp is too heavyweight for this purpose.
 
 Another problem with setjmp/longjmp is that you pay the price for
 every try/catch block whether or not any exception is thrown; I
 only want to pay the price if the exception is actually thrown, as
 they are relatively rare in my application.
 
 Instead, I have a small function implemented with asm() that "throws"
 the exception (this is the x86 version):
 
     // "frame" points to the saved bp of the target stack frame
     // "psize" is the number of bytes of pushed arguments to the
     //    frame above (the function called by the target frame)
     // "pc" is the destination address in the function corresponding
     //    to "frame"
     void
     _jc_restore_stack_frame(_jc_stack_frame *frame, int psize, const void *pc)
     {
 	asm volatile (
 	    "movl %2,%%ebx\n"                   /* %ebx = pc */
 	    "movl %1,%%eax\n"                   /* %eax = psize */
 	    "movl %0,%%ebp\n"                   /* %ebp = frame */
 	    "leave\n"                           /* pop off stack frames */
 	    "popl %%ecx\n"                      /* pop off saved pc */
 	    "addl %%eax,%%esp\n"                /* pop off parameters */
 	    "jmp *%%ebx\n"                      /* continue */
 	    : : "g" (frame), "g" (psize), "g" (pc) : "memory");
     }
 
 The 'pc' is obtained by (admittedly questionably) relying on the fact that
 the .data section is layed out consecutively, and interspersing "location"
 structures in the target code, e.g.:
 
     struct location {
 	const void *pc;
 	int tag;
     }
 
     #define LOCATION(tag) \
     { \
 	__label__ here; \
 	static struct location dummy = { &&here, (tag) }; \
 	here: \
     }
 
     struct location func_locations[] = { };
 
     func()
     {
 	//start of "try" block
 	//do something
 	//end of "try" block
 	goto label1;
 
 	LOCATION(tag);
 	//start of "catch" block
 	//do something
 	//end of "catch" block
 
     label1:
 	// continue doing stuff
     }
 
 This works great except that sometimes GCC eliminates the "catch"
 code because it appears unreachable. So instead I have to do this:
 
     func()
     {
 	// prevent gcc from eliminating code after 'label2'
 	static const void *const reachables[] = { &&lable2 }:
 	if (func == strcmp)
 	    goto *reachables[0];
 
 	//start of "try" block
 	//do something
 	//end of "try" block
 	goto label2;
 
     label2:
 	LOCATION(tag);
 	// start of "catch" block
 	//do something
 	//end of "catch" block
 
     label2:
 	// continue doing stuff
     }
 
 An "__attribute__ ((reachable))" could fix this.. of course what
 I'd really like is a built-in, efficient and more reliable way to
 throw and catch exceptions from within C (my trick won't work on
 some architectures)... any suggestions from GCC/ELF wizards along
 these lines are greatly appreciated.
 
 Thanks,
 -Archie
 
 __________________________________________________________________________
 Archie Cobbs     *     Precision I/O      *     http://www.precisionio.com


             reply	other threads:[~2003-03-26 23:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-26 23:16 Archie Cobbs [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-03-27  0:59 geoffk
2003-03-26 22:16 Zack Weinberg
2003-03-26 21:56 archie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030326230601.30298.qmail@sources.redhat.com \
    --to=archie@dellroad.org \
    --cc=gcc-prs@gcc.gnu.org \
    --cc=nobody@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).