Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!seismo!mimsy!chris From: chris@mimsy.UUCP Newsgroups: comp.lang.c Subject: Re: jump tables (was Re: Why no labelled loops?) Message-ID: <5626@mimsy.UUCP> Date: Sat, 28-Feb-87 20:15:58 EST Article-I.D.: mimsy.5626 Posted: Sat Feb 28 20:15:58 1987 Date-Received: Sun, 1-Mar-87 17:48:16 EST References: <1710@plus5.UUCP> <388@cognos.UUCP> <545@mntgfx.MENTOR.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 99 In article <545@mntgfx.MENTOR.COM> franka@mntgfx.MENTOR.COM (Frank A. Adrian) writes: >Now for my pet peeve. (Well, *he* spells it right. :-) ) >Why can't you take the address of a label? Actually, due to what must be considered a bug in 4BSD PCC, you can: f() { int *p; /* esablish label name: */ if (0) goto foo; p = foo; ... There is a hitch, however: You cannot `goto p'. If you are willing to cheat, it is possible. See the appended bug ... I mean program. >[A jump table] is different [from `switch'] in three ways ... All true enough. >This construct is not difficult to implement on linkers, Also true. >it is not any more hazardous than the goto statement itself, Not so! The existing C `goto' can never change function contexts. Only `longjmp' has this power. Label assignments, unless they are severely restricted, one can do non-local `goto's quite easily. This also means that the label type must carry around a lot of extra baggage, which has a tendency to destroy much of the efficiency you thought you were gaining. >and it introduces a capability into the language which is not >currently there and for which no fast workaround exists. True. >In fact, I wonder why this idea has not been implemented before... I think it has. /* * Program demonstrating weird jump table. * * Passes lint. */ int *labels[3]; main() { register int *addr; /* r11 */ register int i; #ifdef lint #define LABEL(i, l) if (rand()) goto l; else labels[i] = l #else #define LABEL(i, l) if (0) goto l; else labels[i] = l #endif LABEL(0, foo); LABEL(1, bar); LABEL(2, baz); i = 0; loop: if (i < 3) { addr = labels[i++]; #ifdef lint addr = addr; #endif asm(" jmp (r11)"); printf("oops\n"); exit(1); } printf("done\n"); exit(0); foo: printf("foo\n"); goto loop; bar: printf("bar\n"); goto loop; baz: printf("baz\n"); goto loop; } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) UUCP: seismo!mimsy!chris ARPA/CSNet: chris@mimsy.umd.edu