kenny@uiucdcsb.UUCP (07/11/86)
/* Written 10:55 pm Jun 28, 1986 by lambert@mcvax.UUCP in uiucdcsb:net.lang.c */ /* ---------- "Re: RRe: What's so good about FORTH" ---------- */ In article <255@myrias.UUCP> mj@myrias.UUCP (Michal Jaegermann) writes: > The fact that Forth is "threaded" is not a part of a language definition but > side-effect of the most popular implementation method. [...] The method > is far from beeing unique to Forth and there are some Forth implementations > which are not exactly "threaded". [...] An extreme case is to have the > whole Forth as a huge 'case' statement in C. (Yes, I have seen something > like that :-) ). How else can you implement threading if C is used as the implementation language? (Or is that what the smiley is for?) In other languages the obvious implementation would be to have each code fragment (for interpreting a "word") switch to the interpretation of the next word by having it end on goto *(IC++) (or goto *(++IC), depending on your and your machine's taste). But labels are not a C type: you can only goto a constant label, not to a label expression. So the closest you can get--as far as I can see--is to have this huge switch while (1) switch (*(IC++)) { case word1: ...; break; case word2: ...; break; ...; } This is almost as efficient, but not quite: you loose a few cycles in the jump taken at each break. Also, you had better make sure that the words are contiguous numbers so that good code is produced for the switch. (Instead of break you can use continue. In the assembler output of cc this is one jump less, but compiled with -O, at least on our cc, the extra jump is optimized away anyway and the resultant object files are identical.) Are there other ideas on implementing threading in C? -- Lambert Meertens ...!{seismo,okstate,garfield,decvax,philabs}!lambert@mcvax.UUCP CWI (Centre for Mathematics and Computer Science), Amsterdam /* End of text from uiucdcsb:net.lang.c */ Is there something wrong with int ((*foo) ()) [] = { ... list of code addresses for threaded primitives ... } int IP; ... for (;;) (* (foo [IP++])) (); That's how I'd do it. () /\ .-.-. /\ Kevin Kenny ()() < > \ / (__) University of Illinois at Urbana-Champaign /\ \/ V /\ UUCP: {ihnp4,pur-ee,convex}!uiucdcs!kenny "When in doubt, CSNET: kenny@UIUC.CSNET lead trumps." ARPA: kenny@B.CS.UIUC.EDU (kenny@UIUC.ARPA)