From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7745 invoked by alias); 15 Oct 2002 01:38:50 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 7738 invoked from network); 15 Oct 2002 01:38:48 -0000 Received: from unknown (HELO bjl1.asuk.net) (81.29.64.88) by sources.redhat.com with SMTP; 15 Oct 2002 01:38:48 -0000 Received: from bjl1.asuk.net (localhost [127.0.0.1]) by bjl1.asuk.net (8.12.5/8.12.5) with ESMTP id g9F1f2Vn028001; Tue, 15 Oct 2002 02:41:02 +0100 Received: (from jamie@localhost) by bjl1.asuk.net (8.12.5/8.12.5/Submit) id g9F1f1tF027999; Tue, 15 Oct 2002 02:41:01 +0100 X-Authentication-Warning: bjl1.asuk.net: jamie set sender to egcs@tantalophile.demon.co.uk using -f Date: Mon, 14 Oct 2002 21:11:00 -0000 From: Jamie Lokier To: Kevin Lawton Cc: gcc@gcc.gnu.org Subject: Re: Request of new __attribute__ for switch statements (elimination of the bounds check) Message-ID: <20021015014101.GB27718@bjl1.asuk.net> References: <20021011190521.57024.qmail@web80309.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20021011190521.57024.qmail@web80309.mail.yahoo.com> User-Agent: Mutt/1.4i X-SW-Source: 2002-10/txt/msg00773.txt.bz2 I would prefer to have an attribute on enumurated types that says a value of that type is always one of the enum values: enum __attribute__ ((strict_enum)) { CAT, FISH, RABBIT } Pet; (It would be appropriate to add a warning when an enum with this attribute is converted to an integer). Then any switch statement, without adornment, would be able to assume a value of that type is in the range. _If_ every enum label is mentioned in the switch, there is no need for the bounds check. (GCC already checks enum labels in a switch if `-Wswitch' is used, which may be helpful). There would be no bounds in check in code like this: Pet my_pet; /* ... */ switch (my_pet) { case CAT: /* ... */ case FISH: /* ... */ case RABBIT: /* ... */ } The reason I like this is that this attribute could improve the performance of the many programs which use enums, while keeping the GCC-specific attribute in the place where it is least intrusive. Also, many more programs _could_ be changed easily to use enums in place of #defines, and it would be nice to offer a potential performance enhancement for those programs too. You can use this attribute to achieve Kevin's goal of faster threaded interpretation, but it is a bit ugly. For a byte-code dispatch, you'd have to define an enum with 256 scratch names, and cast your byte to that type in the switch. For a sparse dispatch, you'd have to use a different enum type. It's a bit ugly but might be ok with macros. Anyway, I prefer the enum attribute simply because it is useful for many programs in a way which is doesn't intrude on most of the code, i.e. there are usually more switch statements than type definitions. E.g. GCC itself could benefit (lots of switch statements there!). enjoy, -- Jamie