Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Forth has an "inner interpreter" called NEXT

I can't find any reference to this in ANS Forth 1994, other than appendix C. 3 Hardware Implementations of Forth which says: "In the mid-1980’s Zilog developed the z8800 (Super8) which offered ENTER (nest), EXIT (unnest) and NEXT in microcode." (which is interesting, by the way).



Here is a sample from one particular Forth implementation, line 305:

https://github.com/nornagon/jonesforth/blob/master/jonesfort...

This is fairly typical.


> Every FORTH primitive that we write has to be ended by NEXT. Think of it kind of like a return.

Interesting; it's "like a return" because it's a form of continuation: a tail call to the next thing.

I wonder if this could be done in C with function pointers? If we end a function like this:

  typedef void (*fptr_t)(); // old-style, on purpose

  void primitive(fptr_t *ppSelf) // pointer into array of fptr_t's
  {
    // ... perform primitive

    (*++ppSelf)();  // increment to NEXT primitive and call
  }
Will modern C compilers turn this indirect call in tail position into a tail call, like a direct call.


In C you'd try to get an indirect jmp into the body of the low level compiled version of the word. So likely you'd just use an 'asm' instruction and ignore the stackframe that C normally sets up because that would be overhead you do not need.

Alternatively, you could implement your own data stack and use the regular stack for control flow, but this would incur a performance penalty.

See those () at the end of your 'next' are exactly what you don't want, they will mess up the stack. Now you have to start the definition of every word with something that eats up that return address and the stack frame set up at the beginning of the word definition again. You don't really need either of those. But you do need a data stack.


The inner interpreter of Mitch Bradley's portable and efficient CForth is simply implemented with a big switch statement, in the function called "inner_interpreter".

The C compiler typically compiles that code with the big switch statement into an efficient jump table, behind the scenes.

https://github.com/MitchBradley/cforth/blob/master/src/cfort...

Properly speaking, that's a token threaded interpreter, since it switches on token numbers in the code field, instead of indirecting function through pointers to the C equivalent of "code" words.

https://en.wikipedia.org/wiki/Threaded_code#Token_threading

Compiled user defined Forth colon definitions, <builds and does> definitions, etc, do thread through code field address pointers though -- see the default case of the switch statement:

https://github.com/MitchBradley/cforth/blob/master/src/cfort...

At the other end of the spectrum, there's the Novix Forth Engine!

https://news.ycombinator.com/item?id=8860786

>DonHopkins on Jan 9, 2015 | parent | context | favorite | on: Design of Lisp-Based Processors Or, LAMBDA: The Ul...

>The Novix FORTH chip was a pretty cool implementation of FORTH in hardware -- it had separate data and return stacks, which it could push or pop at the same time, so the compiler could combine several FORTH words into one opcode.

http://users.ece.cmu.edu/~koopman/stack_computers/sec4_4.htm...

>The Novix NC4016, formerly called the NC4000, is a 16-bit stack based microprocessor designed to execute primitives of the Forth programming language. It was the first single-chip Forth computer to be built, and originated many of the features found on subsequent designs. Intended applications are real time control and high speed execution of the Forth language for general purpose programming.

>The NC4016 uses dedicated off-chip stack memories for the Data Stack and the Return Stack. Since three separate groups of pins connect the two stacks and the RAM data bus to the NC4016, it can execute most instructions in a single clock cycle.

https://web.archive.org/web/20160402032358/http://www.forth....

>The NC4000P is a single chip FORTH Engine based upon minimum hardware concepts developed by Mr. Charles H. Moore. This highly parallel machine architecture directly executes FORTH primitives in a single clock cycle. The initial implementation of this device is based upon a 4000 gate CMOS semicustom integrated circuit operating at an 8 MHz clock rate.

But I'm preaching to the choir, Jacques! ;) Dadadatablblblblbl!

https://news.ycombinator.com/item?id=3814155

>jacquesm on April 8, 2012 | parent | context | favorite | on: A Forth Story...

>Quite a story, and it seems it does not have a happy ending, but for all the wrong reasons. About 2/3rds in (past the 'read more') the Novix rates a mention. I think that that is possibly the most underrated CPU design that ever saw the light of day, at the time it was so out of the ordinary that only very few people knew what to do with it.

>A high level language (forth is by most definitions a high level language) directly executed by the CPU.

>At the time I worked for a company called dadadata in the Netherlands, they were in the process of developing a bunch of real time software to process video images for classification and recognition (1988 or thereabouts). The Novix chip arrived and would have definitely blown the socks of anything that I could have done with a regular PC at the time, if not for one small problem: someone dropped a dime into the power supply and the whole development kit was toast. A couple of all nighters later we had a working reproduction of the code in 'C' and that was used for the customer demonstrations. Still, the power of that little chip is something I'll never forget and it is a pity that the Novix and its successor which iirc was called shboom never made real headway. The forth code for the novix was probably only about 10% or so in size of the equivalent C code. The head forth honcho there called C sneeringly a 'great' language.

>There is still hope, even today that one day that power will finally be available in some package that sees wide distribution, the heritage of Novix lives on in the greenarrays line (which is now shipping dev boards and chips).

http://www.greenarraychips.com/

>To kill forth and the concepts behind it permanently will take a lot of garlic and stakes, I think it will always have its champions.


I have a hunch that by using GNU C computed labels (extension not in ISO C), you could have an actual NEXT macro which increments the pointer through the label table, and does a goto through the incremented value.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: